简体   繁体   中英

Teradata UNION with additional data

Lets say we have two different Table

Table_eBay

Id      Product      
 1      SomeProduct-1
 2      SomeProduct-2

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

Regards, Sérgio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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