带有附加数据的 Teradata UNION
Teradata UNION with additional data
假设我们有两个不同的 Table
Table_eBay
Id Product
1 SomeProduct-1
2 SomeProduct-2
Table_Amazon
Id Product
1 SomeProduct-1
2 SomeProduct-3
是否可以像下面这样组合?
Table_Output
Id Product isEbay isAmazon
1 SomeProduct-1 TRUE TRUE
2 SomeProduct-2 TRUE FALSE
3 SomeProduct-3 FALSE TRUE
select row_number () over (order by Product) as Id
,Product
,max (case tab when 'E' then 'TRUE' else 'FALSE' end) as isEbay
,max (case tab when 'A' then 'TRUE' else 'FALSE' end) as isAmazon
from ( select 'E' ,Product from Table_eBay
union all select 'A' ,Product from Table_Amazon
) t (tab,Product)
group by Product
order by Product
;
我建议使用如下所示的完整连接:
select
case when AMZ.product is null then EB.product else AMZ.product end as product,
case when AMZ.id is null then 'FALSE' else 'TRUE' end as isEbay,
case when EB.id is null then 'FALSE' else 'TRUE' end as isAmazon,
rownum as ID /* alternative take the last char of product */
from AMAZON AMZ FULL OUTER JOIN EBAY EB ON
AMZ.product = EB.product;
请注意,在您的示例中,产品 ID = 2 表示表中有 2 个不同的产品(我使用了 Oracle 的 rownum)。
此致,
塞尔吉奥
假设我们有两个不同的 Table
Table_eBay
Id Product
1 SomeProduct-1
2 SomeProduct-2
Table_Amazon
Id Product
1 SomeProduct-1
2 SomeProduct-3
是否可以像下面这样组合?
Table_Output
Id Product isEbay isAmazon
1 SomeProduct-1 TRUE TRUE
2 SomeProduct-2 TRUE FALSE
3 SomeProduct-3 FALSE TRUE
select row_number () over (order by Product) as Id
,Product
,max (case tab when 'E' then 'TRUE' else 'FALSE' end) as isEbay
,max (case tab when 'A' then 'TRUE' else 'FALSE' end) as isAmazon
from ( select 'E' ,Product from Table_eBay
union all select 'A' ,Product from Table_Amazon
) t (tab,Product)
group by Product
order by Product
;
我建议使用如下所示的完整连接:
select
case when AMZ.product is null then EB.product else AMZ.product end as product,
case when AMZ.id is null then 'FALSE' else 'TRUE' end as isEbay,
case when EB.id is null then 'FALSE' else 'TRUE' end as isAmazon,
rownum as ID /* alternative take the last char of product */
from AMAZON AMZ FULL OUTER JOIN EBAY EB ON
AMZ.product = EB.product;
请注意,在您的示例中,产品 ID = 2 表示表中有 2 个不同的产品(我使用了 Oracle 的 rownum)。
此致, 塞尔吉奥