发布网友 发布时间:2022-04-08 02:34
共2个回答
懂视网 时间:2022-04-08 06:56
统计连续时间段数据
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时间分段查询
标签:
热心网友 时间:2022-04-08 04:04
中国*境内的一般sqlserver格式化格式是用120,不用126
select * from tablename where CONVERT(VARCHAR(10), CREATEDATE, 120) <= '2018-01-31'而且用不着substring,直接在convert后边的varchar(10)定义截取长度就行,还有你之前为什么用11,2018-01-01本来也就只有10位长度。