declare @start smalldatetime,@end smalldatetime,@sj smalldatetime
create table #tmp(tm smalldatetime,cnt int)
set @start='2012-01-08 00:00:00'
set @end='2012-01-09 00:00:00'
set @sj=dateadd(mi,5,@start)
while @sj<=@end
begin
insert into #tmp(tm,cnt)
select @sj,count(*) from NotificationHistory
where
(LastUpdate between @start and @sj )
and DataGroup = 'Price'
set @start=@sj
set @sj=dateadd(mi,5,@start)
--select @start,@sj
end
select tm,cnt from #tmp order by tm
drop table #tmp
create table #tmp(tm smalldatetime,cnt int)
set @start='2012-01-08 00:00:00'
set @end='2012-01-09 00:00:00'
set @sj=dateadd(mi,5,@start)
while @sj<=@end
begin
insert into #tmp(tm,cnt)
select @sj,count(*) from NotificationHistory
where
(LastUpdate between @start and @sj )
and DataGroup = 'Price'
set @start=@sj
set @sj=dateadd(mi,5,@start)
--select @start,@sj
end
select tm,cnt from #tmp order by tm
drop table #tmp
本文介绍了一种使用SQL的日期函数和条件筛选来高效地在特定时间段内获取数据的方法。通过设置开始时间和结束时间变量,使用DATEADD函数计算下一个时间点,并在循环中迭代查询目标数据集。该方法适用于需要频繁查询特定时间范围内的数据场景,如监控系统或报表生成。
1万+

被折叠的 条评论
为什么被折叠?



