2012-04-16 31 views
1

我有一個表記錄應用程序活動。 每行都包含一個DateTime(「Time」)和一個「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 

什麼是擴展,以相同小時內算多個不同事件類型的最簡單的方法?

::更新

應該已經指定,我沒有帶剛由事件類型藏漢組合,因爲我只希望所有可用的事件類型的子集。 (即沒有無聊的跟蹤/調試數據) 此外,我想不同的事件類型作爲列,而不是額外的行復制DateTime條目。

如...

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

道歉不精確最初的問題!

+0

添加事件類型,選擇列表和組by- – 2012-04-16 09:26:38

回答

4

編輯:

的改變問題的新解決方案:

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 
+0

非常完美,非常感謝! – 2012-04-16 10:20:36

相關問題