【发布时间】:2020-07-02 01:04:46
【问题描述】:
我想在每个task_type 的响应中都有列,计数按date_trunc('day') 和user_id 分组。因此,一旦整个查询运行,它将返回 task_type_1 列,字段值将是给定用户在给定日期具有该类型的任务数。
到目前为止,我有这个运行但不确定如何将 task_type 分组添加到此查询:
SELECT users.id AS user_id,
date_trunc('day', workforce_assigned_tasks.created_at) AS day,
SUM(workforce_assigned_tasks.duration) AS duration,
SUM(workforce_assigned_tasks.earnings_cents) AS earnings_cents,
SUM(workforce_assigned_tasks.subtask_count) AS subtask_count,
WHAT GOES HERE?
FROM users
JOIN workforce_assigned_tasks ON workforce_assigned_tasks.user_id = users.id
JOIN workforce_tasks ON workforce_assigned_tasks.workforce_task_id = workforce_tasks.id
GROUP BY date_trunc('day', workforce_assigned_tasks.created_at), users.id;
【问题讨论】:
-
提供样本数据和期望的结果。
标签: sql postgresql grouping aggregate-functions