我从查询中得到以下输出。我想在一个查询中得到一列包含3列的所有不同值。我无法决定是否可以通过connect或其他函数实现。
Dad Mom Child
------ ---- -----
D006 M026 LA019
D006 M026 NP082
D005 M048 NG073
预期产出:
D006
D005
M026
M048
LA019
NP082
NG073
答案 0 :(得分:1)
使用Union
将不同列合并为单列,这也将避免duplicates
SELECT Dad FROM yourtable
UNION
SELECT Mom FROM yourtable
UNION
SELECT Child FROM yourtable
更新:
WITH cte
AS (SELECT Dad,Mom,Child
FROM bigquery)
SELECT Dad FROM cte
UNION
SELECT Mom FROM cte
UNION
SELECT Child FROM cte;
答案 1 :(得分:1)
与CTE联合更简单,但如果您使用11g,则可以忽略列:
select distinct person
from (
select *
from (
-- this is your real big query
select 'D006' as dad, 'M026' as mom, 'LA019' as child from dual
union all select 'D006', 'M026', 'NP082' from dual
union all select 'D005', 'M048', 'NG073' from dual
)
unpivot (person for anyone IN (dad AS 'DAD', mom AS 'MOM', child AS 'CHILD'))
);
PERSON
------
D006
NP082
M026
LA019
M048
D005
NG073
7 rows selected
unpivot操作添加一个新列,指示值来自哪个原始列;因为你显然不感兴趣它可以被丢弃,但必须在外部查询中完成。
SQL Fiddle demo使用unpivot添加的额外列显示内部查询,并在外部查询中包含相同的内容以删除它并删除重复项。