【问题标题】:SQL Server 2008 R2 - find differences between select statementsSQL Server 2008 R2 - 查找选择语句之间的差异
【发布时间】:2023-03-17 17:05:01
【问题描述】:

如何找到基于同一张表的两个 select 语句之间的差异?例如:

SELECT ID
FROM Table as TableA
WHERE Date = '2016-04-30'

根据上面的select语句,我想找出所有不存在的ID:

SELECT ID
FROM Table as TableB
WHERE Date = '2016-03-31'

【问题讨论】:

    标签: sql-server-2008-r2 not-exists notin


    【解决方案1】:

    较早的答案是一个聪明的答案。这是一种简单的方法。

    Select ID from Table
    where Date = '2016-04-30' 
    and ID not in (select ID from Table where Date = '2016-03-31')
    

    【讨论】:

    • 谢谢。这就是我一直在寻找的。​​span>
    【解决方案2】:
    SELECT ID 
    FROM Table
    group by ID
    having sum(case when Date = '2016-04-30' then 1 else 0 end) > 0
       and sum(case when Date = '2016-03-31' then 1 else 0 end) = 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      相关资源
      最近更新 更多