zoukankan      html  css  js  c++  java
  • SQLServer时间分段查询

    统计连续时间段数据

    if OBJECT_ID(N'Test',N'U') is not null
    	drop table Test
    go 
    
    create table Test(
    	pscode decimal(15),
    	outputcode int,
    	monitortime datetime
    )
    
    insert into Test
    select 4100000406,1,convert(datetime,'2015-04-01 00:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 01:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 02:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 03:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 04:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 05:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 06:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 07:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 08:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 09:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 10:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 11:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 13:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 14:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 15:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 16:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 17:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 18:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 19:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 20:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 22:00') union all
    select 4100000406,1,convert(datetime,'2015-04-01 23:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 01:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 02:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 03:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 04:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 05:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 06:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 07:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 08:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 09:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 11:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 12:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 13:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 14:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 15:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 16:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 17:00') union all
    select 4100000405,2,convert(datetime,'2015-04-01 18:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 00:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 01:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 02:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 03:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 04:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 05:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 06:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 07:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 08:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 09:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 11:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 12:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 13:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 14:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 15:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 16:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 17:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 18:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 19:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 20:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 22:00') union all
    select 4100000402,1,convert(datetime,'2015-04-01 23:00')
    
    
    select pscode,outputcode,
    	   convert(varchar(16),MIN(monitortime),120)+'~'+convert(varchar(16),MAX(monitortime),120) fw,
           COUNT(1) num
      from (select x.pscode,x.outputcode,x.monitortime,dateadd(HOUR,-x.orderby,x.monitortime) diff
    		  from (select pscode,outputcode,monitortime,
    					   ROW_NUMBER() over(partition by pscode,outputcode order by pscode,outputcode,monitortime) orderby 
    				  from Test) x)y 
     group by y.pscode,y.outputcode,y.diff

  • 相关阅读:
    [QT_QML]qml假如调试信息 qDebug console.debug
    [QT_FFMPEG]学习问题: 刚开始移植ffmpeg,测试时出现 undefined reference to `avcodec_configuration()'
    [QT_OPENCV] qt下opencv配置以及首个opencv工程
    [QT][SQLITE]学习记录二 日期查询
    [QT][DEMO] QTableWidget 设置某一列禁止编辑
    [QT][SQLITE]学习记录一 querry 查询
    [QT]QPixmap图片缩放和QLabel 的图片自适应效果对比
    [QT]问题记录-QPixmap::scaled 缩放不成功
    利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现
    利用javascript实现文本的自动输出
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6848922.html
Copyright © 2011-2022 走看看