我想在每个人的回复中都有专栏 task_type
计数按分组 date_trunc('day')
以及 user_id
. 因此,一旦整个查询运行,它将返回 task_type_1
列和字段值将是给定用户在给定日期具有该类型的任务数。
到目前为止,我已经运行了这个命令,但不确定如何将任务类型分组添加到此查询中:
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;
1条答案
按热度按时间u7up0aaq1#
您可以使用条件聚合,它在postgres中使用
FILTER
条款:我猜是的
task_type
在workforce_tasks
.请注意,表别名的使用使查询更易于编写和读取。