sql循环遍历结果/分组/排序

发布于 2024-12-09 21:13:31 字数 1081 浏览 1 评论 0原文

这是我上一篇文章的后续问题,

我有以下代码,它将查询我们的代理在特定时间登录的数据库。它在某一天效果很好,但我现在需要在选定的时间段内执行此操作。我不知道从哪里开始调整!我需要按日期和时间分组的结果...因此它看起来如下所示

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-12-16 21:13:31

像这样的东西(未经彻底测试)

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。

Something like this (not thoroughly tested)

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]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文