【问题标题】:Comparison of two tables in TeradataTeradata 中两个表的比较
【发布时间】:2021-07-02 03:13:33
【问题描述】:

我需要比较 Teradata SQL 中的两个表。

两个表的样本是

表 1

User  Date          Ind1    Ind2
12   2010-05-23     Y       N
12   2010-06-23     Y       Y
12   2010-07-23     Y       N
23   2010-04-23     N       N
23   2010-05-23     N       N
23   2010-06-12     Y       N

表 2

User  Date          Ind1    Ind2
12   2010-05-23     Y       N
12   2010-06-23     Y       Y
12   2010-07-23     N       N
24   2010-03-22     N       N
23   2010-04-23     N       Y
23   2010-05-23     N       Y
23   2010-06-12     Y       N

在此示例中,Table 1Table 2 不同,因为它没有用户 24,而且 Ind1Ind2 中的某些值已更改。我通常比较 Teradata 之外的数据,因为我不熟悉代码。由于表非常大,我想知道是否有办法直接比较这些表(例如,对用户使用联接;我有兴趣查看Ind1=Y 更多的情况见表 1)。

预期输出:

Table 1 (where Ind1 and Ind2 have at least 1 Y)
    User  Date          Ind1    Ind2
    12   2010-05-23     Y       N
    12   2010-06-23     Y       Y
    12   2010-07-23     Y       N
    23   2010-06-12     Y       N

表 2

User  Date          Ind1    Ind2
12   2010-06-23     Y       Y      /* it differs from Ind2 */
12   2010-07-23     N       N      /* it differs from both indicators */
24   2010-03-22     N       N     /* Not included in Table 1 */
23   2010-04-23     N       Y     /* it differs from Ind2 */
23   2010-05-23     N       Y     /* it differs from Ind2 */ 
23   2010-06-12     Y       N     /* it differs from Ind1 */

【问题讨论】:

  • 编辑您的问题并显示您想要的结果。
  • 有几种方法可以比较表,具体取决于您基于 EXCEPT [ALL]、NOT EXISTS 或 FULL JOIN 的预期结果。
  • 如果你想看看,我开了一个新问题:stackoverflow.com/questions/66969965/…

标签: sql teradata teradata-sql-assistant


【解决方案1】:

比较此类表的典型方法是使用full join

select t1.*, t2.*
from table1 t1 full join
     table2 t2
     on t1.user = t2.user and t1.date = t2.date and
        t1.ind1 = t2.ind1 and t1.ind2 = t2.ind2
where t1.user is null or t2.user is null;

这将返回每个表中不存在于另一个表中的行。注意:它不能很好地处理重复行——例如,一个表中的 1 个重复匹配另一个表中的任何数字。

【讨论】:

  • @Math 。 . .这不是这里要问的问题。你可能想问一个新的问题。
【解决方案2】:

您的第一个结果看起来很简单

select *
from table1
where ind1 = 'Y' 
   or ind2 = 'Y'

而且第二个好像是集合操作:

select *
from table2

except

select *
from table1

如果有重复的行,EXCEPT ALL 可能就是您想要的。如果没有重复,它可能仍然比 EXCEPT 更有效,您应该尝试一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多