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
时间: 2025-02-01 21:43:15

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

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

原文:SQLServer访问Oracle查询性能问题解决 1. 问题 系统有个模块,需要查询Oracle数据库中的数据.目前是通过建立链接服务器实现的. SQLServer访问Oracle实现 可参考这篇文章http://www.cnblogs.com/gnielee/archive/2010/09/07/access-oracle-from-sqlserver.html 目前的查询语句就是一个简单的带where条件的查询语句,类似如下: SELECT * FROM LINKED_NAME..A

浅谈SQLServer 时间格式化

SQLServer 时间格式化 背景知识: SQL Server自带的Convert?函数,可以将日期类型的字段按照指定的格式转换为字符串格式,您可以将以下脚本复制到查询分析器执行; DECLARE @now datetime SET @now = GETDATE() select convert(nvarchar(MAX), @now, 0) as output, 0 as style union select convert(nvarchar(MAX), @now, 1), 1 union

MySQL时间分组查询

表TESTER 字段:id -- INT    date  -- TIMESTAMP 1.如何按年.月.日分组查询? select DATE_FORMAT(date,'%Y-%m-%d') time, count(*) count from TESTER group by year(date), month(date), day(date); 其中year().month().day()分别是提取date中的年.月.日字段. 2.时间分组查询的效率? 在不建立索引时,我100W行数据进行测试,用

一个经典的数据库分段查询和分组查询综合运用的例子

记得以前面试时遇到的,感觉这个问题比较经典,所以后来整理了下. 题目描述是这样的(凭印象): 请用一条sql统计出每个班级中,20-50分.50-70分.70-100分的人数, 已知:表(exam),有如下字段:class(班级).name(姓名).score(分数): 查询显示格式: 这是个典型的分段又分组的查询案例,难点就是考虑到按班级分组的同时按成绩分段查询. 表数据如下: select count_abc.class 班级, sum(case when count_abc.a is nu

SQLSERVER 时间函数汇总

1.求当天的年份 (getdate(): 2012/05/08 18:07:26)    SELECT YEAR(GETDATE())     --20122. 求当天的月份       SELECT MONTH(GETDATE())   --53. 求当天的日     SELECT DAY(GETDATE())      --84. 求年月日    SELECT CONVERT(VARCHAR,GETDATE(),112)  --结果:20120508    SELECT CONVERT(VA

SQL语句关于时间的查询小心得,希望大家给点意见

查询本月信息:Select * FROM T_Users Where datediff(month,RegisterTime,getdate())=0 昨天的信息:SELECT * FROM T_Users where LastLoginTime>DATEADD(DAY,-2,GETDATE()) AND LastLoginTime<GETDATE() SELECT SUM([DetailMoney]) FROM [dbo].[T_BuyDetails] INNER JOIN [dbo].[T

SqlServer中循环查询结果集

declare @id int,@value nvarchar(100); begin declare c_test_main cursor fast_forward for select hps_hpId,hps_time from hospitalPermSupp where hps_usId=1; open c_test_main;--打开游标 while 1=1 --开始循环 begin fetch next from c_test_main into @id,@value; --赋值到

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

sqlserver中怎么查询字段为空的记录的两种方法:

Oracle、MySql、SQLServer 数据分页查询

Oracle.MySql.SQLServer 数据分页查询 摘自:http://www.cnblogs.com/wangyong/p/3396333.html 近简单的对oracle,mysql,sqlserver2005的数据分页查询作了研究,把各自的查询的语句贴出来供大家学习..... (一). mysql的分页查询 mysql的分页查询是最简单的,借助关键字limit即可实现查询,查询语句通式: /* * sql:可以是单表的查询语句,也可以是多表的联合查询语句 * firstIndex: