统计连续时间段数据

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

SQLServer时间分段查询的更多相关文章

  1. Sqlserver数据库分页查询

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  2. SQLServer访问Oracle查询性能问题解决

    原文:SQLServer访问Oracle查询性能问题解决 1. 问题 系统有个模块,需要查询Oracle数据库中的数据.目前是通过建立链接服务器实现的. SQLServer访问Oracle实现 可参考 ...

  3. MySQL时间分组查询

    表TESTER 字段:id -- INT    date  -- TIMESTAMP 1.如何按年.月.日分组查询? select DATE_FORMAT(date,'%Y-%m-%d') time, ...

  4. sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)

    sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...

  5. Oracle、MySql、SQLServer数据分页查询

    看过此博文后Oracle.MySql.SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: 别名.字段 FROM( SELECT row_nu ...

  6. SQL设置SQLServer最大连接数查询语句

    设置最大连接数 下面的T-SQL 语句可以配置SQL Server 允许的并发用户连接的最大数目. exec sp_configure 'show advanced options', 1exec s ...

  7. sql语句中日期时间格式化查询

          今天在做会员管理系统搜索时,我发现以前的搜索时间方式不太科学,效率也不是太高.由其是在查询指定的时间相等的时候,我在数据库中都存这样的时间格式"2007-5-22 14:32:1 ...

  8. Solaris下怎样改动文件创建时间及查询

    Solaris下怎样改动文件创建时间及查询 实验演示: 1.核对时间 [root@S1011:/]# date Tue Jul 15 21:37:01 CDT 2014 --若时间不对请先按例如以下格 ...

  9. sqlserver中怎么查询字段为空的记录

    sqlserver中怎么查询字段为空的记录的两种方法: 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https://cnblogs.com/qzf/

随机推荐

  1. java web 学习笔记 - servlet02

    1.servlet的跳转 客户端跳转: 通过doget函数中的response参数调用resp.sendRedirect(url); 代码如下 protected void doGet(HttpSer ...

  2. 如何优雅地从CSDN转载文章

    复制粘贴应该是最显而易见的方法,但是不仅会有丢失内容,而且格式也会丢失.要想达到更好的效果,可以从html源码入手. 1.在chrome浏览器中打开要转载的文章,右键选择检查 2.在chrome的右方 ...

  3. Js 之图片懒加载插件

    一.PC端(lazyload) 1.引入js文件 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.m ...

  4. JavaSE-20 IO序列化

    学习要点 定义 IO如何序列化 序列化 序列化:是将对象的状态存储到特定存储介质中的过程. 反序列化:从特定存储介质中的数据重新构建对象的过程. 实现了java.io.Serializable接口的类 ...

  5. python circle nested

    #!/usr/bin/python # -*- coding:utf- -*- # @filename: tmp2 # @author:vickey # @date: // : def circle_ ...

  6. 基于短语的统计机器翻(PBMT) 开源工具 :Moses

    如何运行Moses 1. Moses的历史 Moses是Pharaoh的升级版本,增加了许多功能.它是一个基于短语的统计机器翻译系统,整个系统用C++语言写成,从训练到解码完全开放源代码,可以运行在L ...

  7. 笔试算法题(01):字符串倒置 & 八皇后问题

    出题:将字符串“ABCD1234efgh”进行前后对调: 分析: 常见的考查指针使用的案例,知道字符串长度之后,依次交换位置i以及位置(length-1-i)上的内容,直到重叠: 注意不能直接修改指针 ...

  8. 【转】WinAPI: CreateFontIndirect - 根据字体结构建立逻辑字体

    //声明: CreateFontIndirect( const p1: TLogFont {字体结构} ): HFONT; {返回新字体指针} //TLogFont 是 tagLOGFONTA 结构的 ...

  9. Nginx(alias 和 root的区别)

    Nginx(alias 和 root的区别)1.alias 和 root 的区别: location /request_path/image { root /local_path/image/; } ...

  10. Python之面向对象方法

    Python之面向对象方法 property的用法: property属于类的封装的范畴 property是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值. 用property的方法,就可 ...