日期:2014-05-17  浏览次数:20576 次

我想写个存储过程,取出某个时间段的第一条数据,请请大侠,高手们指点一下,谢谢!!!
我想写个存储过程,取出某个时间段的第一条数据,请请大侠,高手们指点一下,谢谢!!!
项目要求如下:
原表的数据为:
getInfoTime           waterHeight
2013-5-28 10:00:00    3
2013-5-28 10:02:00    3
2013-5-28 10:05:00    3
2013-5-28 10:06:00    3
2013-5-28 10:09:00    3
2013-5-28 10:10:00    3
2013-5-28 10:11:00    3
2013-5-28 10:13:00    3
2013-5-28 10:15:00    3
2013-5-28 10:17:00    3
2013-5-28 10:19:00    3
2013-5-28 10:20:00    3
2013-5-28 10:25:00    3
2013-5-28 10:26:00    3
存储过程有3个参数开始时间,结束时间,时间间隔
假如
开始时间为:2013-5-28 09:58:00 
结束时间为:2013-5-28 10:30:00 
时间间隔为:10分钟
我怎样才能把
2013-5-28 09:58:00  ----- 2013-5-28 10:08:00之间第一条的数据2013-5-28 10:00:00    3取出来
2013-5-28 10:08:00  ----- 2013-5-28 10:18:00之间第一条的数据2013-5-28 10:09:00    3取出来
2013-5-28 10:18:00  ----- 2013-5-28 10:28:00之间第一条的数据2013-5-28 10:19:00    3取出来

------解决方案--------------------
时间间隔是用来干嘛的
------解决方案--------------------

declare @tb table(
getInfoTime datetime,
waterHeight int
)
declare @begintime datetime,@endtime datetime
select @begintime='2013-5-28 09:58:00',@endtime = '2013-5-28 10:30:00'
insert @tb
select '2013-5-28 10:00:00',3
union all select '2013-5-28 10:02:00',3
union all select '2013-5-28 10:05:00',3
union all select '2013-5-28 10:06:00',3
union all select '2013-5-28 10:09:00',3
union all select '2013-5-28 10:10:00',3
union all select '2013-5-28 10:11:00',3
union all select '2013-5-28 10:13:00',3
union all select '2013-5-28 10:15:00',3
union all select '2013-5-28 10:17:00',3
union all select '2013-5-28 10:19:00',3
union all select '2013-5-28 10:20:00',3
union all select '2013-5-28 10:25:00',3
union all select '2013-5-28 10:26:00',3

;with tb as(
select *,datediff(ss,@begintime,getInfoTime)/600 dtdiff from @tb
where getInfoTime >=@begintime and getInfoTime <=@endtime
)

select getInfoTime,waterHeight from tb a
where not exists(
select 1 from tb b 
where a.dtdiff = b.dtdiff 
and a.getInfoTime > b.getInfoTime
)