这是我上一篇文章的后续问题
我有以下代码,它将查询我们的代理在特定时间登录的数据库。它适用于某一天,但我现在需要在选定的时间段内完成。我不知道从哪里开始调整这个!我需要按日期和小时分组的结果...所以它看起来如下
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) )
任何想法!? 非常感谢
答案 0 :(得分:0)
像这样的东西(未经过彻底测试)
alter procedure [dbo].[LoggedOnCountByHour]
@FromDayToCheck datetime,
@ToDayToCheck datetime,
@HelplineID int
as
select dateadd(hour, N.number, @FromDayToCheck) as [date_hour],
DATEPART(hh,dateadd(hour, N.number, @FromDayToCheck)) 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, @FromDayToCheck) and
L.LoginTime < dateadd(hour, N.number + 1, @FromDayToCheck)
left join PoolMembership P on P.ExpertRecID = L.ExpertRecID
where N.Type = 'P' and
dateadd(hour, N.number + 1, @FromDayToCheck) <= @ToDayToCheck + 1 and
P.HelplinePoolID = @HelplineID
group by dateadd(hour, N.number, @FromDayToCheck), DATEPART(hh,dateadd(hour, N.number, @FromDayToCheck) )
order by [date_hour], [Hour]
注意:如果您认为您所检查的时间间隔超过2048小时,则应使用数字表而不是master..spt_values。