在一个时间段内的间隔查询
我想在一个时间段内然后按照间隔时间来查询数据
select * from 监控温度表 where 监控温度表.时间 between ' " + Str(dtStart.Value) + " 'AND ' " + Str(dtEnd.Value) + " 'order by id
这个是在一个时间段内查询,现在要加个时间间隔来查询,不知所云,望高手解答
------解决方案----------------------间隔时间能求出来,但如果没有该记录,取其最近记录就不好求了.
if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(dt datetime, val int)
insert into tb(dt,val) values( '2007-01-01 00:00:00 ' , 1)
insert into tb(dt,val) values( '2007-01-01 00:05:00 ' , 2)
insert into tb(dt,val) values( '2007-01-01 00:10:00 ' , 3)
insert into tb(dt,val) values( '2007-01-01 00:15:00 ' , 4)
insert into tb(dt,val) values( '2007-01-01 00:18:00 ' , 5)
insert into tb(dt,val) values( '2007-01-01 00:20:00 ' , 6)
insert into tb(dt,val) values( '2007-01-01 00:21:00 ' , 7)
insert into tb(dt,val) values( '2007-01-01 00:22:00 ' , 8)
insert into tb(dt,val) values( '2007-01-01 00:30:00 ' , 9)
insert into tb(dt,val) values( '2007-01-01 00:05:00 ' , 10)
insert into tb(dt,val) values( '2007-01-01 00:40:00 ' , 11)
insert into tb(dt,val) values( '2007-01-01 00:50:00 ' , 12)
insert into tb(dt,val) values( '2007-01-01 00:51:00 ' , 13)
insert into tb(dt,val) values( '2007-01-01 00:52:00 ' , 14)
insert into tb(dt,val) values( '2007-01-01 00:55:00 ' , 15)
go
declare @dt1 as datetime
declare @dt2 as datetime
set @dt1 = '2007-01-01 00:00:00 '
set @dt2 = '2007-01-01 01:00:00 '
declare @间隔 as int --间隔时间,
set @间隔 = 5 --5分钟
select * from tb where dt > = @dt1 and dt <= @dt2 and datediff(minute , dt , @dt1) % @间隔 = 0
drop table tb
/*
dt val
------------------------------------------------------ -----------
2007-01-01 00:00:00.000 1
2007-01-01 00:05:00.000 2
2007-01-01 00:10:00.000 3
2007-01-01 00:15:00.000 4
2007-01-01 00:20:00.000 6
2007-01-01 00:30:00.000 9
2007-01-01 00:05:00.000 10
2007-01-01 00:40:00.000 11
2007-01-01 00:50:00.000 12
2007-01-01 00:55:00.000 15
(所影响的行数为 10 行)
*/
------解决方案--------------------郁闷
啊
接分了