繁体   English   中英

Teradata UNION和其他数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM