[英]Teradata UNION with additional data
Lets say we have two different Table 假设我们有两个不同的表
Table_eBay Table_eBay
Id Product
1 SomeProduct-1
2 SomeProduct-2
Table_Amazon Table_Amazon
Id Product
1 SomeProduct-1
2 SomeProduct-3
Is that possible to combine like below? 可以像下面这样组合吗?
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
;
I recommend using a full join as below: 我建议使用完全联接,如下所示:
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;
Take into account that in your example the product ID = 2 means 2 different products in the tables (i used rownum from Oracle). 考虑到在您的示例中,产品ID = 2表示表中有2种不同的产品(我使用了Oracle的rownum)。
Regards, Sérgio 问候,塞尔吉奥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.