【问题标题】:how find the patttern of missing columns in sql如何在sql中找到缺失列的模式
【发布时间】:2023-02-07 01:26:56
【问题描述】:

我有两张桌子。我必须检查为什么其他表 2 中缺少表 1 中的每一行。

table1
     ID                   col1        col2        col3         date    
     -------------------  ---------  ----------  -----------  ------------ 
     1000                 AAA        B         212           20220712     
     1001                 ALC        B         40            20210528     
     1002                 BBBB       B         13            20210528     
     1003                 UUU        B         299           20210528     
     1004                 EEE        S         36            20210707     
     1005                 CCC        S         695           20220420
     
     
 table2    
     ID                   col1        col2        col3         date    
     -------------------  ---------  ----------  -----------  ------------ 
     1000                 AAA        B         212           20220712     
     1001                 AAA        B         40            20210528     
     1002                 BBBB       B         13            20210428     
     1003                 UUU        B         299           20210528     
     1006                 EEE        S         36            20210707     
     1005                 CCC        B         695           20220520



Result table:

       ID                   col1       col2        col3         date           Reason                          
     -------------------  ---------  ----------  -----------  ------------   -------------------
     1001                 ALC        B         40            20210528         Col1 Change
     1002                 BBBB       B         13            20210528         Date Change    
     1005                 CCC        S         695           20220420         Col2 and Date Change
     1004                 EEE        S         36            20210707         ID change/whole row missing

例如, 1.ID 1000 匹配两个表。这不得不被忽略。 2.ID 1001 仅在 col1 中更改,必须在“Col1 changed”列中报告 3.ID 1002 仅在日期上更改,必须在“日期更改”栏中报告 4.ID 1005 在 col2 和 date 中发生了变化,这必须在“date and col2 changed”列中报告 5. 如果有任何东西在 table2 中而不在 table1 中,则可以忽略。必须报告 table1 和 table2 之间的所有非匹配行。

【问题讨论】:

  • 什么 RDBMS 和你试过什么?
  • 我正在使用 Microsoft sql server。我正在尝试将左连接一个接一个地连接起来,但到目前为止效果并不好。
  • 你试过外部连接吗?
  • 结果中不应该包含 id 1004 的行吗?
  • @cid.. 是的。应该包括在内。我错过了..

标签: sql


【解决方案1】:

这是我要滚动的内容:

首先在每个不匹配或为空的列上外部连接两个表,然后使用案例表达或者内联if报告列是否不匹配;你可以利用这个功能concat_ws在这里为 Reason 构建一个字符串:

select t1.*, 
    case when t2.Id is null then 'ID change/whole row missing' else
    Concat_ws(' and ',
        Iif(t1.col1 = t2.col1, null, 'Col1 change'),
        Iif(t1.col2 = t2.col2, null, 'Col2 change'),
        Iif(t1.col3 = t2.col3, null, 'Col3 change'),
        Iif(t1.date = t2.date, null, 'Date change')
    )
end Reason
from t1
left join t2 on t1.Id = t2.Id
where not(
        t1.col1 = t2.col1 and t2.col1 is not null
    and t1.col2 = t2.col2 and t2.col2 is not null
    and t1.col3 = t2.col3 and t2.col3 is not null
    and t1.date = t2.date and t2.date is not null
);

看到这个工作demo fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-13
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多