基于while循环的SQL查询
我正在尝试使用 while 循环构建一个 sql 查询,每次迭代将日期时间增加一分钟,然后根据时间生成一个 select 语句:
declare @dt datetime
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
select Count(*) From Actions Where Timestamp = @dt
set @dt = DATEADD(mi, 1, @dt)
end
该查询按预期工作,除了 while 循环的每次迭代似乎都会产生一个新的完全查询,而不是简单地查询一个新行。有没有一种方法可以构造这个,以便它的一个查询和每一行都是通过循环的增量生成的? 我相信发生这种情况是因为 select 语句位于循环内部,但我不确定如何以不同的方式构建它。
编辑-这是我使用临时表想到的,但它很慢。也许有更快的方法?如果不是这样也没关系,至少这是有效的:
create table #temp
(
[DT] datetime not null,
[Total] int not null
)
declare @dt datetime
declare @result int
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
set @result = Count(*) From Actions Where Timestamp = @dt
insert #temp ([DT],[Total]) values (@dt, @result)
set @dt = DATEADD(mi, 1, @dt)
end
select * from #temp;
drop table #temp;
I am trying to construct an sql query using a while loop that increments a datetime by one minute each iteration and then generates a select statement based on the time:
declare @dt datetime
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
select Count(*) From Actions Where Timestamp = @dt
set @dt = DATEADD(mi, 1, @dt)
end
The query works as intended except that every iteration of the while loop seems to produce a new query entirely, rather than simply a new row. Is there a way to construct this so that its one single query and each row is generated by the incrementation of the loop?
I believe this occurs because the select statement is inside the loop, but I'm not sure how to construct it a different way that works.
EDIT - Here is what I came up with using a temporary table, but it is slow. Maybe there is a faster way? If not thats fine, atleast this works:
create table #temp
(
[DT] datetime not null,
[Total] int not null
)
declare @dt datetime
declare @result int
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
set @result = Count(*) From Actions Where Timestamp = @dt
insert #temp ([DT],[Total]) values (@dt, @result)
set @dt = DATEADD(mi, 1, @dt)
end
select * from #temp;
drop table #temp;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用数字表的一种方法
如果您有自己的数字表,请使用该表
请参阅此处了解更多信息 http://wiki.lessthandot.com/index.php/Date_Ranges_Without_Loops
你的完整查询就像
One way by using a table of numbers
If you have your own number table, use that
See here for more info http://wiki.lessthandot.com/index.php/Date_Ranges_Without_Loops
you full query would be like
如果您有一个数字表(从 0 到一百万或其他),这相对简单:
不需要循环。
有一些方法可以优化它,但这应该是可以理解的。
另请注意,时间戳不能有任何秒或几分之一秒才能正常工作(您的原始版本也有此问题)。
If you have a numbers table (from 0 to a million or whatever), this is relatively simple:
No need for loops.
There's ways to optimize this, but that should be fairly understandable as it is.
Also note that the timestamps cannot have any seconds or fractions of a second for this to work (your original has this problem as well).