[英]Teradata UNION with additional data
假設我們有兩個不同的表
Table_eBay
Id Product
1 SomeProduct-1
2 SomeProduct-2
Table_Amazon
Id Product
1 SomeProduct-1
2 SomeProduct-3
可以像下面這樣組合嗎?
表格輸出
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)。
問候,塞爾吉奧
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.