PostgreSQL - 一个计算用户每天所做行动的查询

时间:2014-11-01 14:50:48

标签: sql postgresql

我有一个包含以下字段的表:userId,actionDate,actionType 我想创建一个查询,以生成每个用户每天执行的操作列表

我已尝试过此查询:

select userId, actionDate, count(*) from userActionList group by userId, actionDate;

问题:是我的actionDate是一个日期时间字段,我根据日期和时间得到结果时间,我希望它只根据日期分组(忽略时间)

1 个答案:

答案 0 :(得分:1)

这是您似乎要求的查询修改:

select userId, date_trunc('day', actionDate) as actionDate, count(*)
from userActionList
group by userId, date_trunc('day', actionDate);

这会计算活动。如果你想要一个清单:

select userId, date_trunc('day', actionDate) as actionDate, count(*),
       string_agg(actionType, '; ' order by actionDate) as actions
from userActionList
group by userId, date_trunc('day', actionDate);