简体   繁体   English

如何将具有相同数据类型的两个不同列的结果放在一列中,以使两个表中的两行在目标表中都是唯一的

[英]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:假设 colA、colB 列是字符串,则应首先将值拆分为原子值 SPLIT_TO_TABLE 并再次组合 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: Output:

在此处输入图像描述


Sidenote: For storing non-atomic values ARRAY is a better choice:旁注:对于存储非原子值 ARRAY 是更好的选择:

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 :然后组合是使用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: Output:

在此处输入图像描述

Consider a UNION query:考虑一个 UNION 查询:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) 如何将两个表连接在一起并返回两个表中的所有行,以及如何将它们的某些列合并为一个列 - How to join two tables together and return all rows from both tables, and to merge some of their columns into a single column 当两个表中的一个列名相同时,sql查询从两个表中获取数据 - sql query to get data from two tables when one column name is same in both tables 比较两行(都具有不同的 ID)并检查它们的列值是否完全相同。 所有行和列都在同一个表中 - Compare two rows (both with different ID) & check if their column values are exactly the same. All rows & columns are in the same table 计算来自不同表的特定值的行,两个表都包含同一列? - count rows for particular value from different table both table contains one same column? 如何从两个不同的表中获取两个不同的列,并将其放入具有一对一映射关系的第三个表中? - How to fetch two different columns from two different tables and put it into a third table with one to one mapping? SQL对来自两个不同表的行进行计数,其中两个表都具有完全相同的外键 - SQL count rows from two different tables where both have same foreign key from entirely different table 如何从两个表中都有一个公共列的两个表的多个列中选择数据? - how do i select data from multiple columns from two tables where there is a common column in both? 是否可以将两个表连接到一个表中,两个表具有相同的列名...? - Is it possible to join two tables into one single table , both tables have the same column names ...? 连接来自两个不同表的两个列,其中两个列都有部分相似的数据 - Join two columns from two different tables where both the columns have partially similar data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM