zoukankan      html  css  js  c++  java
  • 记一次SQL连续时间间隔分组汇总的问题

    实现日期间隔分组, 间隔小于等于3s的数据为一组,数据源如下

    create table #tmptable(id nvarchar(20),dd date ,dt  datetime)
    go
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:01')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:02')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:03')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:04')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:05')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:06')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:07')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:09')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:11')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:12')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:15')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:19')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:20')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:24')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:25')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:26')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:27')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:28')
    insert #tmptable values('1','2010-1-1','2010-1-1 00:00:29')
    insert #tmptable values('1','2010-1-2','2010-1-2 00:00:36')
    insert #tmptable values('1','2010-1-2','2010-1-2 00:00:37')
    insert #tmptable values('1','2010-1-2','2010-1-2 00:00:48')
    insert #tmptable values('1','2010-1-2','2010-1-2 00:00:59')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:09')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:11')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:12')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:15')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:19')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:20')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:24')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:25')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:26')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:27')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:28')
    insert #tmptable values('2','2010-1-1','2010-1-1 00:00:29')
    insert #tmptable values('2','2010-1-2','2010-1-2 00:00:36')
    insert #tmptable values('2','2010-1-2','2010-1-2 00:00:37')
    insert #tmptable values('2','2010-1-2','2010-1-2 00:00:48')
    insert #tmptable values('2','2010-1-2','2010-1-2 00:00:59')
    
    go
    

      检测数据断点

    ;WITH MyList AS(SELECT ROW_NUMBER() OVER(ORDER BY id,dd, dt asc) RN,T.*  FROM #tmptable T) 
    select a.RN,a.id,a.dd,a.dt,
    	CASE WHEN b.dt IS null then 0
    		ELSE DATEDIFF(ss,b.dt,a.dt) END as secspan,
    	CASE WHEN b.dt IS null or DATEDIFF(ss,b.dt,a.dt) <= 3 THEN 1 ELSE 0
    	 END as flag
    	 --into #TMP1
     from MyList a left join MyList b
    on b.RN = a.RN - 1 and a.id = b.id and a.dd  = b.dd
    

     

    聚合时间段

    select id,dd,MIN(dt) as sdt,MAX(dt) as edt from 
    (
    	select ss=(select SUM(flag) from #TMP1 where dt <= a.dt and id = a.id and dd = a.dd)
    	,* from #TMP1 a where id= a.id and dd = a.dd
    ) a
    GROUP BY (RN - ss),id,dd
    

     

     

  • 相关阅读:
    pl2303 驱动
    tomcat 启动脚本
    Linux下Shell命令加减乘除计算
    定时删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件
    mysql 拼接字符
    jquery iframe父子框架中的元素访问方法
    在线工具
    js对数组对象的操作以及方法的使用
    HTML 设置字体
    10月1号 备忘录
  • 原文地址:https://www.cnblogs.com/dpwow/p/8758706.html
Copyright © 2011-2022 走看看