我有一个表格列表"动作",其中每个动作都有一个时间和执行它的人。我喜欢的是知道每天执行任何行动(并不重要)的不同人的数量。所以我的表看起来像:
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)
(或某些特定于数据库的版本; sqlite中的group by strftime('%Y-%m-%d', action_time)
等),我也可以按person
进行分组......但是{{1为第一天做事的人数提供 4 ...即,它计算行数,而不是人数 (这是2,而不是4)。我不认为我正确理解select date(action_time), count(person) from actions group by date(action_time)
。我该怎么办?
答案 0 :(得分:0)
SELECT DATE(action_time), COUNT(DISTINCT person) FROM ACTIONS GROUP BY DATE(action_time);