sql循环遍历结果/分组/排序
这是我上一篇文章的后续问题,
我有以下代码,它将查询我们的代理在特定时间登录的数据库。它在某一天效果很好,但我现在需要在选定的时间段内执行此操作。我不知道从哪里开始调整!我需要按日期和时间分组的结果...因此它看起来如下所示
date hour count
10/10/11 22 52
10/10/11 23 24
11/10/11 00 12
11/10/11 01 33
因此将为所选范围内的每个日期显示 24 小时期限。
ALTER procedure [dbo].[LoggedOnCountByHour]
@DayToCheck datetime,
@HelplineID int
as
select dateadd(hour, N.number, @DayToCheck) as [date_hour],
DATEPART(hh,dateadd(hour, N.number, @DayToCheck)) as [Hour],
count(L.ExpertRecID) as [count of users]
from master..spt_values as N
left outer join WorkDetail as L
on L.KickedOffTime > dateadd(hour, N.number, @DayToCheck) and
L.LoginTime < dateadd(hour, N.number + 1, @DayToCheck)
left join PoolMembership P on P.ExpertRecID = L.ExpertRecID
where N.Type = 'P' and
N.Number between 0 and 23 and
P.HelplinePoolID = @HelplineID
group by dateadd(hour, N.number, @DayToCheck), DATEPART(hh,dateadd(hour, N.number, @DayToCheck) )
有什么想法吗!? 非常感谢
this is a follow on question from my previous post
I have the following code which will query a database of when our agents were logged on at a particular time. It works nicely for a given day, but I need to now do it for a selected time period. I am not sure where to start to adjust this! I'd need the results grouped by date and hour... so it would look as follows
date hour count
10/10/11 22 52
10/10/11 23 24
11/10/11 00 12
11/10/11 01 33
So the 24 hour period would be displayed for each date within the selected range.
ALTER procedure [dbo].[LoggedOnCountByHour]
@DayToCheck datetime,
@HelplineID int
as
select dateadd(hour, N.number, @DayToCheck) as [date_hour],
DATEPART(hh,dateadd(hour, N.number, @DayToCheck)) as [Hour],
count(L.ExpertRecID) as [count of users]
from master..spt_values as N
left outer join WorkDetail as L
on L.KickedOffTime > dateadd(hour, N.number, @DayToCheck) and
L.LoginTime < dateadd(hour, N.number + 1, @DayToCheck)
left join PoolMembership P on P.ExpertRecID = L.ExpertRecID
where N.Type = 'P' and
N.Number between 0 and 23 and
P.HelplinePoolID = @HelplineID
group by dateadd(hour, N.number, @DayToCheck), DATEPART(hh,dateadd(hour, N.number, @DayToCheck) )
any ideas!?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西(未经彻底测试)
注意:如果您认为您检查的时间间隔超过 2048 小时,您应该使用数字表而不是 master..spt_values。
Something like this (not thoroughly tested)
Note: If you think that your time interval you are checking against is more than 2048 hours you should use a numbers table instead of master..spt_values.