Teradata UNION附加数据

时间:2016-12-02 08:32:19

标签: sql teradata

让我们说我们有两个不同的表

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

2 个答案:

答案 0 :(得分:1)

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
;

答案 1 :(得分:0)

我建议使用完整联接,如下所示:

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)。

此致 塞尔吉奥