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 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
SQL

SQL join: unnesting rows with same column names (but different data) without resorting to aliases

Let’s say I have three tables: t1: client_id transmission_id timestamp column_A 1 AAA1 2024-10-16 10:31:27 Banana 1 AAA2 2024-10-16 11:31:27 Citrus 2 BBB1 2024-10-16 09:12:14 Apple t2: client_id transmission_id timestamp column_B 1 AAA1 2024-10-16 10:41:27 Paris 1 AAA2 2024-10-16 11:41:27 London 2 BBB1 2024-10-16 09:22:14 NY t3: client_id transmission_id timestamp column_C 1 AAA1 2024-10-16 10:31:27

Read More
SQL

PostgreSQL how to merge rows where some fields match and others are null

I have some data that looks like the below: id category sub_category sub_sub_category 1 Category 1 null null 1 Category 1 Subcategory 1 null 1 Category 1 Subcategory 1 SubSubCategory 1 1 Category 1 Subcategory 1 SubSubCategory 2 1 Category 2 Subcategory 2 SubSubCategory 2 I need it to look like this: id category sub_category

Read More
SQL

Not getting correct result with Oracle xmltable to parse the XML

I need to parse the below XML to get the result in the form of the below table. I am getting NULL column without any data row. I am expecting below 3 rows with 3 columns. Kindly help thanks in advance. Required Output UNIFIER_US_XCPG1_VIEW 1 2024-10-16T08:36:53.000+00:00 UNIFIER_UXCORY_VIEW 5 2024-10-16T08:36:53.000+00:00 UNIFIER_UXBLS_VIEW 9 2024-10-16T08:36:53.000+00:00 Query: WITH tbl

Read More
SQL

Which Postgres schema should I go with?

This is my first time working with databases. I’m designing a database for an e-commerce project. I ended up with two versions of the schema, and I don’t know which one to go with or what its pros and cons are. V1: -- Person Schema create table person ( id serial primary key, name text

Read More
SQL

Remove the all comments from a sql view in azure DW

CREATE VIEW [dimension].[v_dim_customer] as SELECT — primary key KNA1.fk_MANDT_KUNNR AS sk_customer, KNA1.bk_MANDT_mandant, — attribut KNA1.[NAME1_nom], KNA1.[NAME2_nom_2], — KNA1.[DEAR1_concurrents], –KNA1.[DEAR2_responsable_adv], ----- /*flag client interne/externe*/----- flag_ext_int.externe_interne_code, /* flag client interne/externe niveau 2 (détaillé : intra groupe / intra société / externe)*/ flag_ext_int.externe_interne_level_2_code, -- date pour delta --CY.max_slt_datetime -- add --18/12/2023 FROM [e3p].[KNA1] KNA1 LEFT OUTER JOIN [e3p].T005T

Read More
SQL

Subquery of select * vs Subquery of select fields

Need help in understanding which of the following snowflake SQL is faster and why. provided tablea has 50 columns. Or do You think both are equally efficient. select col1, col2 from (select * from tablea /*AdditionalLogc*/)) --Vs select col1, col2 from (select col1, col2 from tablea /*AdditionalLogc*/)) You need to sign in to view this

Read More
SQL

Getting Table Column Value Based on Dynamic Column Name

I have a ‘column specifier’ table that specifies the names of 1 or more columns of a source table that contain the data needed for stuff. The list of columns required depends on various things, but here, it depends only on what I’m calling the ‘cid’. I must retrieve the values found in each column

Read More
SQL

Counting Active Transactions in SQL: Issues with Status Logic in Time Series Data

I am trying to create a SQL query to count active transactions month by month from a transaction_status table. Each transaction_id can have multiple statuses, with two key statuses: ENDORSEMENT_SUCCESS (when the transaction is active) and ENDORSEMENT_CLOSED_SUCCESS (when the transaction is closed). Problem: The challenge I’m facing is ensuring that: A transaction should only be

Read More