【问题标题】:Separating data if time difference is 24 hours如果时差为 24 小时,则分离数据
【发布时间】:2020-12-23 05:42:12
【问题描述】:

我有一张如下表,

id giftcard_id downltime
1111 6 23-03-2017
1111 7 01-04-2017
1111 7 02-04-2017
1111 7 05-05-2017
1111 7 06-05-2017
1111 7 07-05-2017
1111 7 08-05-2017
1111 7 11-05-2017
2222 8 21-07-2018
2222 8 22-07-2018
2222 8 23-07-2018
2222 8 28-09-2019

如果时差超过 24 小时(1 天),我希望礼品卡记录在单独的行中。我想要如下的输出,

id giftcard_id startTime endTime
1111 6 23-03-2017 23-03-2017
1111 7 01-04-2017 02-04-2017
1111 7 05-05-2017 08-05-2017
1111 7 11-05-2017 11-05-2017
2222 8 21-07-2018 23-07-2018
2222 8 28-09-2019 28-09-2019

【问题讨论】:

  • 只是检查为什么 21-07 到 23-07 不是一个单一的记录,因为它们代表连续的时间间隔
  • 你试过什么?你在哪里卡住了?向我们展示你的尝试。
  • @GeorgeJoseph,我的错。 21 到 23 应该排成一排。你是对的
  • @DaleK,我尝试使用 LAG 但它不起作用
  • @Ali 所以告诉我们

标签: sql sql-server database tsql ssms


【解决方案1】:

这就是孤岛和间隙问题。

您可以按如下方式使用解析函数和聚合:

SELECT ID, GIFTCARDID, 
       MIN(DOWNLTIME) AS STARTTIME, 
       MAX(DOWNLTIME) AS ENDTIME 
  FROM (SELECT T.*, 
               SUM(CASE WHEN DOWNLTIME = DATEADD(DAY,1,LG) THEN 0 ELSE 1 END)  
                    OVER (PARTITION BY ID, GIFTCARDID ORDER BY DOWNLTIME) AS SM 
          FROM (SELECT T.*, 
                       LAG(DOWNLTIME) 
                         OVER (PARTITION BY ID, GIFTCARDID ORDER BY DOWNLTIME) AS LG
                  FROM YOUR_TABLE T) T
        )
  GROUP BY ID, GIFTCARDID, SM

【讨论】:

    【解决方案2】:

    这是一种使用 tabibitosan 方法的方法

    with data
      as (
    select *
     from (values
          (1111,6,convert(date,'23-03-2017',105))
        ,(1111,7 ,convert(date,'01-04-2017',105))
        ,(1111,7 ,convert(date,'02-04-2017',105))
        ,(1111,7 ,convert(date,'05-05-2017',105))
        ,(1111,7 ,convert(date,'06-05-2017',105))
        ,(1111,7 ,convert(date,'07-05-2017',105))
        ,(1111,7 ,convert(date,'08-05-2017',105))
        ,(1111,7 ,convert(date,'11-05-2017',105))
        ,(2222,8 ,convert(date,'21-07-2018',105))
        ,(2222,8 ,convert(date,'22-07-2018',105))
        ,(2222,8 ,convert(date,'23-07-2018',105))
        ,(2222,8 ,convert(date,'28-09-2019',105))
        )t(id,giftcardid,downltime)
         )
     select id
           , giftcardid
           , min(downltime) as starttime
           , max(downltime) as endtime
    from (
            select id
                   , giftcardid
                   , downltime
                   , row_number() over(partition by id order by downltime) as rnk
                   , dateadd(day,-row_number() over(partition by id order by downltime),downltime) as grp_val
              from data
         )x
    group by x.id
           , x.giftcardid
           , x.grp_val
    
    
    +------+------------+------------+------------+
    |  id  | giftcardid | starttime  |  endtime   |
    +------+------------+------------+------------+
    | 1111 |          6 | 2017-03-23 | 2017-03-23 |
    | 1111 |          7 | 2017-04-01 | 2017-04-02 |
    | 1111 |          7 | 2017-05-05 | 2017-05-08 |
    | 1111 |          7 | 2017-05-11 | 2017-05-11 |
    | 2222 |          8 | 2018-07-21 | 2018-07-23 |
    | 2222 |          8 | 2019-09-28 | 2019-09-28 |
    +------+------------+------------+------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2020-08-07
      相关资源
      最近更新 更多