那麼這不是一個非常明確的標題,是嗎?SQLite:爲列中的每個不同值找到前n個聚合
我有一個SQLite表results
:
event | dayOfWeek | hour| eventCount
--------+-----------+-----+------------
Event A | 0 | 0 | 4926
Event A | 0 | 1 | 1492
...
Event A | 1 | 0 | 7372
Event A | 1 | 1 | 49
...
Event B | 0 | 0 | 234648
...
它只包含的時間每個事件發生在一週的每一天的每一個小時數。
我一直在建立一個表daily
這樣的:
create table daily as
select event,
sum(case when dayOfWeek = 0 then count else 0 end) as sunday,
sum(case when dayOfWeek = 1 then count else 0 end) as monday,
sum(case when dayOfWeek = 2 then count else 0 end) as tuesday,
sum(case when dayOfWeek = 3 then count else 0 end) as wednesday,
sum(case when dayOfWeek = 4 then count else 0 end) as thursday,
sum(case when dayOfWeek = 5 then count else 0 end) as friday,
sum(case when dayOfWeek = 6 then count else 0 end) as saturday
from results
group by event;
爲了得到一個表,看起來像這樣:
event |sunday|monday|tuesday|wednesday|thursday|friday|saturday
--------+------+------+-------+---------+--------+------+---------
Event A | 345 | 2345 | 341 | 568 | 689 | 2351 | 1455
...
其中只包含計數爲每個事件類型中的每一天星期。在一天中的每小時和每天/每小時建立一個類似的表是微不足道的,並且我有兩個表可用。
我想提出一個表topTenPerHour
這樣的:
hour | 1st | 2nd | 3rd | ...
-----+---------+---------+---------+------
0 | Event A | Event C | Event B | ...
1 | Event B | Event D | Event C | ...
...
23 | Event A | Event R | Event D | ...
但我無法看到如何。有什麼建議麼?編輯:我實際上並不需要創建一個表(我只需要做一個SELECT調用),所以SQLite對CREATE TABLE的限制(如JOIN的不可用性)不適用於此問題。
感謝您的建議,@Alexander!我實際上正在處理完全靜態的歷史數據。我也只創建一個有問題的表格用於報告。數據庫將不會在項目完成後維護。實際上,我將數據存儲在與您的建議非常相似的佈局中。我只是爲演示和分析而創建中間表。這是一個非常大的數據庫,並且擁有這些中間表可極大地減少我的查詢的運行時間。對於如何使用當前佈局製作表格,您有什麼建議嗎? –