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

How can I set identity_insert on a tablename passed as a variable

I’m using the below sql queries in my stored procedure to copy the records from existing table to duplicate table and ended up at the below error Script: -- Step 4: Temporarily allow identity insert on the new table SET @sql="SET IDENTITY_INSERT " + @new_table + ' ON' EXEC sp_executesql @sql -- Step 5: Copy

Read More
SQL

How to rewrite an nested IFF statement to oracle case when statement

How to rewrite the following nested IFF statement to oracle Case when statement . IIF(IN_RNK = 1, IIF(IN_SUM > 2,IIF(IN_MTR_COVERAGE_NUMBER_A = 'A','ORIGINAL BASE','ORIGINAL RIDER'),IN_MTR_COVERAGE_NUMBER_A), IN_MTR_COVERAGE_NUMBER_A) You need to sign in to view this answers

Read More
SQL

Query that adds horizontal and vertical

I have data in a table that consists of expense statements by year. I want to make a SQL query that gives me the sum of the amounts but for a year on a line like in the Excel table that I provided. I tried but I can’t do it. Only one line per year

Read More
SQL

MYSQL Return all rows with additional column for max value for the set without grouping

I know it is weird to try and get the MAX value without a GROUP BY, but maybe there is a way to do what I need. Lets say I have the following table… UserID UserName UserScore 1 John 35 2 Steve 20 3 Chris 40 4 Paul 65 5 Frank 45 What I need

Read More
SQL

How can I add conditions in athena view definition in order to modify column values?

I have a data warehouse in s3 with a bronze, silver and gold layer where the data is saved in delta format. I need to create a gold table from a silver table, and have decided that the best way to do this would be to create a view in athena. The view needs to

Read More
SQL

How to get a value according to its number of occurrence

I have a Key-Value table like this: How to get a value according to the occurrence index of the key. I only got the key index using select INSTR ( key, 'key5<>' , 1,1 ) from table ; How should I get the complementary value to the delimiter occurrence? I want to get the parallel

Read More
SQL

combine SQL queries

I have multiple queries to get data marked under different categories, with it returning the last X rows from before a given datetime. The max timestamp value will be different for each of the categories SELECT * FROM table WHERE category="a" AND timestampCol < timestampValue1 ORDER BY timestampCol DESC LIMIT 3; SELECT * FROM table

Read More
SQL

Select unrelated in a n:m relationship

I have a typical many-to-many relationship resolved by a join table, as shown here: For this question the sample data is: People: PersonID Name P001 Alice P002 Bob P003 Carlos P004 David Courses: CourseID Course C001 Algebra C002 Biology C003 Chemistry Attendance: CourseID PersonID C001 P001 C001 P002 C001 P003 C002 P002 C002 P003 C003

Read More
SQL

How to query database with strict order?

I have raw data on a database with measurements on sensors a, b, c like this for example: unique_id;sensor;timestamp 111;A;2024-10-10 08:00:00.000000 222;C;2024-10-10 08:00:00.000000 333;A;2024-10-10 08:00:00.000000 444;A;2024-10-10 08:00:00.000000 555;A;2024-10-10 08:00:00.000000 666;C;2024-10-10 08:00:01.000000 777;C;2024-10-10 08:00:01.000000 888;C;2024-10-10 08:00:01.000000 555;B;2024-10-10 08:00:09.000000 111;A;2024-10-10 08:00:21.000000 444;A;2024-10-10 08:00:24.000000 444;A;2024-10-10 08:00:38.000000 444;B;2024-10-10 08:00:40.000000 666;C;2024-10-10 08:00:42.000000 111;C;2024-10-10 08:00:42.000000 444;B;2024-10-10 08:00:48.000000 111;C;2024-10-10 08:01:02.000000 666;C;2024-10-10 08:01:02.000000

Read More
SQL

Stream and Task not loading the data into the target table in Snowflake

I have created the external stage in Snowflake as below: CREATE or REPLACE STAGE sk_demo_stage URL='s3://raw-data/' FILE_FORMAT=(type = csv) STORAGE_INTEGRATION = s3_int DIRECTORY = ( ENABLE = TRUE); For a full load, I am using the below syntax: COPY INTO sk_demo.sk_schema.sk_employees FROM @sk_demo_stage/sample/employees; And since CDC is enabled for MySQL and the first column indicates

Read More