【问题标题】:Teradata UNION with additional data带有附加数据的 Teradata UNION
【发布时间】:2016-12-02 16:51:51
【问题描述】:

假设我们有两个不同的表

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

【问题讨论】:

  • 期待新的Id 是什么?
  • 不关心身份。
  • 现在有一些 SO 问题吗?我试图写一个答案,但编辑不接受,我不想把它写在一行中......
  • 所以从预期结果中删除 id!
  • @jarlh (1) 尝试使用 https:// 连接到站点 (2) OP 可能意味着 Id 是任意的,但他仍然想要一个 Id 列

标签: sql teradata


【解决方案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
;

【讨论】:

    【解决方案2】:

    我建议使用如下完全联接:

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

    问候, 塞尔吉奥

    【讨论】:

    • (1) 当 UNION ALL 解决方案更干净且性能更好时,为什么要推荐 FULL JOIN? (2) 你应该使用 COALESCE (3) Teradata 中没有rownum
    • @DuduMarkovitz:当然(2)和(3)是正确的,但是为什么你认为UNION ALL 表现更好呢?这种方法避免了两行的低效聚合。
    • @dnoeth,你是完全正确的。我的思绪在别处:-)
    猜你喜欢
    • 2013-06-10
    • 2016-03-14
    • 2017-12-01
    • 2017-02-16
    • 2017-05-03
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多