1
我有一個表格列出了「操作」,其中每個操作都有時間和執行操作的人員。我想知道的是每天執行任何操作(不管哪個操作)都是不同的人數。所以,我的表如下所示:計算組中的不同行
action_time | person | action
--------------------+--------+-------
2017-05-10 10:00:00 | john | logged on
2017-05-10 10:30:00 | paul | logged on
2017-05-10 17:50:00 | john | logged off
2017-05-10 18:00:00 | paul | logged off
2017-05-11 12:00:00 | john | read email
2017-05-12 12:00:00 | john | read email
2017-05-13 10:00:00 | john | read email
2017-05-13 10:30:00 | paul | read email
2017-05-13 10:45:00 | george | read email
,我希望能夠得到,因爲結果
day | count of people doing things
-----------+-----------------------------
2017-05-10 | 2 (note: john and paul each did 2 different things; don't care)
2017-05-11 | 1 (note: john only)
2017-05-12 | 1 (note: john only)
2017-05-13 | 3 (note: john, paul, and george)
很顯然,我可以GROUP BY DATE(action_time)
(或某些特定的DB-版本; group by strftime('%Y-%m-%d', action_time)
SQLite中,等),我也可以通過組person
以及... select date(action_time), count(person) from actions group by date(action_time)
給出爲第一天的人做事的數量...即它是計數的行數,而不是人數 (這是2,而不是4)。我不認爲我正確理解count()
。我應該在這裏做什麼?
'count(distinct person)'是你正在嘗試做的事情。 –
是的,是的。謝謝@vkp;如果你這樣寫,我會很樂意接受這個答案,或者如果你願意,我可以回答自己。 – sil