简体   繁体   中英

How to put the result of two different columns with same data type within one column such that both rows from both tables are unique in target table

For example I have two columns:

Column A: dog, cat, mouse

Column B: truck, jeep, lorry

I want a situation where:

Column C : dog, truck, cat, jeep, mouse, lorry

I am using Snowflake

Assuming that columns colA, colB are strings, the values should be first splitted to atomic values SPLIT_TO_TABLE and combined again LISTAGG:

SELECT ID, COLA, COLB, LISTAGG(COL, ', ') AS colC
FROM (
  SELECT ID, COLA, COLB, TRIM(s1.VALUE::STRING) AS col
  FROM tab
  ,TABLE(SPLIT_TO_TABLE(tab.colA, ',')) AS s1
  UNION
  SELECT ID, COLA, COLB, TRIM(s2.VALUE::STRING) AS col
  FROM tab
  ,TABLE(SPLIT_TO_TABLE(tab.colB, ',')) AS s2
) AS sub
GROUP BY ID, COLA, COLB
ORDER BY ID;

For sample data:

CREATE OR REPLACE TABLE tab
AS
SELECT 1 AS id, 'dog, cat, mouse' AS colA, 'truck, jeep, lorry' AS colB UNION 
SELECT 2 AS id, 'sparrow' AS colA, 'sparrow, parrot' AS colB;

Output:

在此处输入图像描述


Sidenote: For storing non-atomic values ARRAY is a better choice:

CREATE OR REPLACE TABLE tab
AS
SELECT 1 AS id, ['dog', 'cat', 'mouse'] AS colA, ['truck', 'jeep', 'lorry'] AS colB UNION 
SELECT 2 AS id, ['sparrow'] AS colA, ['sparrow', 'parrot'] AS colB;

在此处输入图像描述

Then combining is a matter of using ARRAY_UNION_AGG :

SELECT ID, ARRAY_UNION_AGG(COL) AS COLC
FROM (
  SELECT ID, COLA AS col FROM tab
  UNION ALL
  SELECT ID, COLB AS col FROM tab
) sub
GROUP BY ID
ORDER BY ID;

Output:

在此处输入图像描述

Consider a UNION query:

SELECT 1 AS GrpID, FieldA AS Data FROM tablename
UNION SELECT 2, FieldB FROM tablename;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM