zoukankan      html  css  js  c++  java
  • SQL经典问题:找出连续日期及连续的天数

    create table tmptable(rq datetime)
    go
    insert tmptable values('2010.1.1')
    insert tmptable values('2010.1.2')
    insert tmptable values('2010.1.3')
    insert tmptable values('2010.1.6')
    insert tmptable values('2010.1.7')
    insert tmptable values('2010.1.10')
    insert tmptable values('2010.1.11')
    insert tmptable values('2010.1.12')
    insert tmptable values('2010.1.19')
    insert tmptable values('2010.1.20')
    insert tmptable values('2010.1.22')
    insert tmptable values('2010.1.23')
    insert tmptable values('2010.1.28')
    go
    ---希望得到的结果
    --本期起始日期 本期终止日期  持续天数 距上一期天数
    --2010.1.1     2010.1.3      3        0
    --2010.1.6     2010.1.7      2        3
    --2010.1.10    2010.1.12     3        3
    --2010.1.19    2010.1.20     2        7
    --2010.1.22    2010.1.23     2        2
    --2010.1.28    2010.1.28     1        5
    SELECT 本期起始日期 = MIN(rq)
        , 本期终止日期 = MAX(rq)
        , 持续天数 = MAX(id1) - MIN(id1) + 1
        , 距上一期天数 = CASE a.id1 - a.id2
            WHEN -1 THEN 0
            ELSE MAX(datediff(d, rq2, rq))
        END
    FROM (
        SELECT id1 = datediff(d, '2010-01-01', rq)
            , id2 = (
                SELECT COUNT(1)
                FROM tmptable
                WHERE rq <= a.rq
            ), rq2 = (
                SELECT MAX(rq)
                FROM tmptable
                WHERE rq < a.rq
            )
            , *
        FROM tmptable a
    ) a
    GROUP BY a.id1 - a.id2
    
    /*
    本期起始日期                  本期终止日期                  持续天数        距上一期天数
    ----------------------- ----------------------- ----------- -----------
    2010-01-01 00:00:00.000 2010-01-03 00:00:00.000 3           0
    2010-01-06 00:00:00.000 2010-01-07 00:00:00.000 2           3
    2010-01-10 00:00:00.000 2010-01-12 00:00:00.000 3           3
    2010-01-19 00:00:00.000 2010-01-20 00:00:00.000 2           7
    2010-01-22 00:00:00.000 2010-01-23 00:00:00.000 2           2
    2010-01-28 00:00:00.000 2010-01-28 00:00:00.000 1           5
    警告: 聚合或其他 SET 操作消除了空值。
     
    (6 行受影响)
    */
     

     例2:

    create table t (qdate datetime,vcode varchar(50));
    insert into t values('2013-06-01','A001');
    insert into t values('2013-06-02','A001');
    insert into t values('2013-06-02','B001');
    insert into t values('2013-06-05','A001');

    生成表如下:

     

    按照vcode进行分组,按照qdate进行降序排列,记录行号rn

    select *,ROW_NUMBER() over (partition by vcode order by qdate desc ) as rn from [my_exercise].[dbo].[t]

    将日期减去行号,得到的结果rn连续相同时即为时间连续组

    select *, (day(qdate) - row_number() over(partition by t.vcode order by t.qdate))rn from [my_exercise].[dbo].[t]

    根据vcode和rn分组,得到的count即为连续的天数

    select vcode,rn,count(*)as count 
    from (
        select t.*,(day(t.qdate) - row_number()over(partition by t.vcode order by t.qdate)) rn 
        from [my_exercise].[dbo].[t]) a 
    group by vcode, rn

    通过having即可筛选出连续天数>=3的vcode

    select vcode,rn,count(*)as count 
    from (
        select t.*,(day(t.qdate) - row_number()over(partition by t.vcode order by t.qdate)) rn 
        from [my_exercise].[dbo].[t]) a 
    group by vcode, rn
    having count(1)>=3
  • 相关阅读:
    一起谈.NET技术,C#调试心经(续) 狼人:
    基于xmpp openfire smack开发之openfire介绍和部署[1]
    mysql学习笔记二
    使用jQuery实现的网页版的个人简历
    基于xmpp openfire smack开发之smack类库介绍和使用[2]
    基于色彩恒常( color constancy)特性的FrankleMcCann Retinex图像增强。
    The import org.cocos2dx.lib cannot be resolved
    C++编译器的递归深度与程序优化思考
    jquery实战视频教程_选项卡效果一
    编译器是怎样工作的?用lex和yacc 写一个计算器(2)
  • 原文地址:https://www.cnblogs.com/zhaoshujie/p/9594648.html
Copyright © 2011-2022 走看看