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
SQLServer时间分段查询的更多相关文章
- Sqlserver数据库分页查询
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
- SQLServer访问Oracle查询性能问题解决
原文:SQLServer访问Oracle查询性能问题解决 1. 问题 系统有个模块,需要查询Oracle数据库中的数据.目前是通过建立链接服务器实现的. SQLServer访问Oracle实现 可参考 ...
- MySQL时间分组查询
表TESTER 字段:id -- INT date -- TIMESTAMP 1.如何按年.月.日分组查询? select DATE_FORMAT(date,'%Y-%m-%d') time, ...
- sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)
sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...
- Oracle、MySql、SQLServer数据分页查询
看过此博文后Oracle.MySql.SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: 别名.字段 FROM( SELECT row_nu ...
- SQL设置SQLServer最大连接数查询语句
设置最大连接数 下面的T-SQL 语句可以配置SQL Server 允许的并发用户连接的最大数目. exec sp_configure 'show advanced options', 1exec s ...
- sql语句中日期时间格式化查询
今天在做会员管理系统搜索时,我发现以前的搜索时间方式不太科学,效率也不是太高.由其是在查询指定的时间相等的时候,我在数据库中都存这样的时间格式"2007-5-22 14:32:1 ...
- Solaris下怎样改动文件创建时间及查询
Solaris下怎样改动文件创建时间及查询 实验演示: 1.核对时间 [root@S1011:/]# date Tue Jul 15 21:37:01 CDT 2014 --若时间不对请先按例如以下格 ...
- sqlserver中怎么查询字段为空的记录
sqlserver中怎么查询字段为空的记录的两种方法: 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https://cnblogs.com/qzf/
随机推荐
- 10 Steps To be a senior programmer
What 软件工程师的职业生涯要历经以下几个阶段:初级.中级,最后才是高级.这篇文章主要是讲如何通过 10 个步骤助你成为一名高级软件工程师. Why 得到更多的报酬!因为你的薪水会随着你水平的提高而 ...
- 用 vue cli 脚手架搭建单页面 Vue 应用(进阶2)
1.配置 Node 环境. 自行百度吧. 安装好了之后,打开 cmd .运行 node -v .显示版本号,就是安装成功了. 注:不要安装8.0.0以上的版本,和 vue-cli 不兼容. 我使用的 ...
- 【计算机网络】3.2 无连接运输:UDP
第三章第二节 无连接运输:UDP UDP(用户数据报协议,User Datagram Protocol),它只是做了运输层协议能够做的最少工作,除了多路复用和多路分解及一些差错检测外,它几乎没有做任何 ...
- 精准判断是360、IE和其他浏览器
function myexplorer(){ var explorer = window.navigator.userAgent; if (!!window.ActiveXObject || &quo ...
- [Python3网络爬虫开发实战] 1.2.5-PhantomJS的安装
PhantomJS是一个无界面的.可脚本编程的WebKit浏览器引擎,它原生支持多种Web标准:DOM操作.CSS选择器.JSON.Canvas以及SVG. Selenium支持PhantomJS,这 ...
- js 技巧 (十)广告JS代码效果大全 【3】
3.[允许关闭] 与前面两个代码不同的是,广告图下方增加了一个图片按纽,允许访客点击关闭广告图片,下面文本框中就是实现效果所需代码: var delta=0.015; var coll ...
- Zabbix微信告警
Zabbix微信告警 摘要 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...
- ArcGIS:Hello World Maps
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- 全文搜索(A-3)-推荐系统构建步骤
用户研究 用户建模 系统建造
- expdp,impdp,include,exclude
The examples below are based on:- the demo schema SCOTT that is created with script: $ORACLE_HOME/rd ...