1

我有一个表记录应用程序活动。每行包含一个日期时间(“时间”)和一个“事件类型”列......(显然还有一些其他在这里不重要)

我希望能够计算每小时发生的不同 EventType 的数量。

我目前正在获取单个 EventType 的基本计数:

select DATEADD(hh, (DATEDIFF(hh,0,Time)),0) as 'hour', count(*) as 'apploads' 
from PlaySessionEvent
where EventType = 0
Group By DATEADD(hh,(DATEDIFF(hh,0,Time)),0)
order by hour

将其扩展为在同一小时内计算多个不同 EventType 的最简单方法是什么?

::更新

应该指定,我还没有只按 EventType 分组,因为我只想要所有可用 EventType 的子集。(即不乏味的跟踪/调试数据)此外,我希望将不同的事件类型作为列,而不是复制 DateTime 条目的附加行。

例如...

DateTime           EventType1        EventType2
12:12:12 12/12/12  45                22

为不准确的初始问题道歉!

4

1 回答 1

4
select EventType, DATEADD(hh, (DATEDIFF(hh,0,Time)),0) as 'hour', count(*) as 'apploads'  
from PlaySessionEvent 
where EventType = 0 
Group By DATEADD(hh,(DATEDIFF(hh,0,Time)),0), EventType
order by hour 

编辑:

已更改问题的新解决方案:

select DATEADD(hh, (DATEDIFF(hh,0,Time)),0) as 'hour', count(*) as 'apploads', 
sum(case when EventType = 1 then 1 else 0 end) EventType1,
sum(case when EventType = 2 then 1 else 0 end) EventType2
from PlaySessionEvent 
where EventType = 0 
group By DATEDIFF(hh,0,Time)
order by hour 

这是一种稍微不同的写法:

select DATEADD(hh, (DATEDIFF(hh,0,Time)),0) as [hour], 
       COUNT(*) [apploads], 
       COUNT(case when EventType = 1 then 1 end) EventType1,
       COUNT(case when EventType = 2 then 1 end) EventType2
from PlaySessionEvent 
where EventType = 0 
group By DATEDIFF(hh,0,Time)
order by hour 
于 2012-04-16T09:27:28.663 回答