【问题标题】:Sql Where there is records with same field values but different timestampSql 哪里有字段值相同但时间戳不同的记录
【发布时间】:2018-07-09 09:14:08
【问题描述】:

我正在研究如何在 where 子句中写入字段值相同但时间戳不同的记录。

表格看起来像这样

ID       NotificationID        DateTime
1        55555                 2018-01-29 23:00:36.983
2        55555                 2018-01-25 18:38:09.513
3        55555                 2018-01-25 18:38:09.513
4        55555                 2018-01-25 18:38:09.513

如果有像 ID = 1 这样的记录,它的 NotificationID#2, #3, #4 相同,但日期时间不同,则为 true

如果表中只有 #2, #3, and #4 具有相同的 NotificationID 和 DateTime,则为 false

我怎样才能在 where 子句中写一些东西来告诉我像 ID#1 这样的记录是真的。

感谢您的帮助

【问题讨论】:

    标签: sql sql-server select exists


    【解决方案1】:

    但是,缺少所需的输出,要查找第二行具有相同 NotificationID 但不同 timestamp 的行,您可以使用 EXISTS

    select ID, NotificationID, timestamp
    from your_table t1
    where exists(
      select 1
      from your_table t2
      where t1.NotificationID = t2.NotificationID and
            t1.timestamp != t2.timestamp
    )
    

    【讨论】:

      【解决方案2】:

      请试试这个 -- 我已经为你生成了两个解决方案。

      CREATE TABLE notify
      (
           ID       INT
          ,NotificationID INT
          ,[DateTime] DATETIME
      )
      GO
      
      INSERT INTO notify VALUES
      (1,55555,'2018-01-29 23:00:36.983'),
      (2,55555,'2018-01-25 18:38:09.513'),
      (3,55555,'2018-01-25 18:38:09.513'),
      (4,55555,'2018-01-25 18:38:09.513')
      
      INSERT INTO notify VALUES 
      (6,11111,GETDATE()),
      (7,11111,GETDATE())
      

      输出 1

      SELECT Id,NotificationID,[DateTime],CASE WHEN a <> b THEN 'False' else 'True' End co FROM 
      (
          SELECT * , COUNT(*) OVER (PARTITION BY NotificationID) a , COUNT(*) OVER (PARTITION BY NotificationID,[DateTime]) b
          FROM notify
      )k
      
      Id          NotificationID DateTime                co
      ----------- -------------- ----------------------- -----
      6           11111          2018-01-30 18:44:18.170 True
      7           11111          2018-01-30 18:44:18.170 True
      2           55555          2018-01-25 18:38:09.513 False
      3           55555          2018-01-25 18:38:09.513 False
      4           55555          2018-01-25 18:38:09.513 False
      1           55555          2018-01-29 23:00:36.983 False
      
      (6 rows affected)
      

      输出 2

      SELECT NotificationID,MAX(CASE WHEN a <> b THEN 'False' else 'True' End) co 
      FROM 
      (
          SELECT * , COUNT(*) OVER (PARTITION BY NotificationID) a , COUNT(*) OVER (PARTITION BY NotificationID,[DateTime]) b
          FROM notify
      )k GROUP BY NotificationID
      
      
      
      NotificationID co
      -------------- -----
      11111          True
      55555          False
      
      (2 rows affected)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-15
        • 2012-02-04
        • 1970-01-01
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多