October 22, 2024
Chicago 12, Melborne City, USA

SQL

Structured Query Language (SQL) is a language for querying databases. Questions should include code examples, table structure, sample dat

SQL

Adding null check to join causes non-null rows to disappear

With the following query: select id, c.actor_id, d.actor_id from community c left join duplicate_community_rows_by_name_actor_id d on d.actor_id = c.actor_id where c.actor_id like '%gameboy%'; I get the results (where <null> is an actual null value): 151492,https://lemm.ee/c/gameboy,<null> 627389,https://lemmy.world/c/gameboy,https://lemmy.world/c/gameboy 55519,https://lemmy.world/c/gameboy,https://lemmy.world/c/gameboy If I modify my query to select results that do not have a null actor_id: select id, c.actor_id,

Read More
SQL

SQL state: 42P01 Detail: There is an entry for table, but it cannot be referenced from this part of the query

I am returning to SQL after decades of not using it, so I have a bit of a noob question. Here is my code: CREATE TEMPORARY TABLE tempo (LIKE tb_old INCLUDING ALL); COPY tempo (id, notes, word) FROM 'C:\www\export_lastline.csv' WITH (FORMAT CSV, HEADER); INSERT INTO tb_old (word, line_break) SELECT word, 'TRUE' FROM tempo WHERE tempo.id

Read More
SQL

Improving query speed in PostgreSQL for large datasets with multiple filters

I’m relatively new to PostgreSQL and have hosted a PostgreSQL database on a remote AWS server. I use this server to analyze derivatives data (futures and options) using Python. I’ve created two separate tables to store options data (one for call options and one for put options), and a table for futures data. Below are

Read More
SQL

SQL Sum of the same ID rows

Each service has the same price and ID. In final table I would sum quantity of the same rows – each service appear only one time and complete ID and price. I created example table https://dbfiddle.uk/Z1EQca0b and I can only return service name and quantity SELECT Service_name, SUM (Qty) As Sum FROM NewOne GROUP BY

Read More
SQL

How to Split a String by Delimiters and Handle Empty Values as NULL in Oracle PL/SQL?

I have a string in Oracle PL/SQL that looks like this: 280,1,2,3,3 | 120,,0,2,3 | 280,1,2,3,3 Each section is separated by a | character, and within each section, values are separated by commas. The challenge I’m facing is correctly extracting the values, and for missing or empty values For example, the data I expect is:

Read More
SQL

Joining different tables with the same columns but different data

I have 4 tables all with the same layout. ID, Name, Totals Each table may have unique and non-unique IDs if the ID and Name is reused but with different totals. For example; Year1 ID Name Totals 1 Bob 15 2 John 2 3 Smith 4 4 Carl 4 Year2 ID Name Totals 1 Bob

Read More
SQL

How can I optimize a slow JOIN query in MySQL?

I’m working with two large tables in MySQL, and I’m trying to retrieve data using a JOIN query. However, the query is taking a long time to execute, especially when the dataset grows. Here’s the query I’m running: Code SELECT users.name, orders.order_date, orders.total FROM users JOIN orders ON users.id = orders.user_id WHERE orders.total > 100;

Read More
SQL

foregin key initialzation

create table bod(refcode char(3) constraint Fk_1 foreign key(refcode) references stafflist(refcode) on update cascade, staff_name varchar(20), qualification varchar(20), phno int, address varchar(30)); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘foreign key(refcode) references stafflist(refcode) on update

Read More
SQL

How to visualize properly BLOB on Oracle SQL?

I have BLOB columns in an Oracle Database that come from an IBM AS400 Database. The raw data look like (this is litterally what i have from my column (for column Data Type and and a row value)) : (BLOB) I’m looking to visualize these BLOBs in a simple query. I mainly used these two

Read More
SQL

Generate a Series of Numbers

I am trying to generate random serial numbers in PostgreSQL with some conditions. The numbers need to start with the same 3 letters, YAK. After YAK, there needs to be 8 random digits. I only need to do 10 iterations of this. I know I should try to start with generate_series() but can I use

Read More