我有以下查询,这些查询计算按周创建的SELECT w.week, COUNT(doctor_profiles.*) AS total
FROM (SELECT generate_series(min(DATE_TRUNC('week', created_at)),
max(DATE_TRUNC('week', created_at)),
interval '7 day'
) as week
FROM doctor_profiles
) w LEFT JOIN
"doctor_profiles"
ON DATE_TRUNC('week', created_at) = w.week
GROUP BY w.week
ORDER BY w.week;
条记录数
approved
我还有一个专栏COUNT(doctor_profiles.approved) as approved
,我希望每周添加一笔总和。我尝试添加doctor_profiles
(布尔字段),但计数返回与doctor_profiles
相同的数字。如何查询在该时间段结束前已批准的所有grep "this is a test" * | wc -l
的总和?
答案 0 :(得分:1)
你可以这样做:
SELECT w.week, COUNT(*) AS total, SUM(approved::int) as approved
FROM (SELECT generate_series(min(DATE_TRUNC('week', created_at)),
max(DATE_TRUNC('week', created_at)),
interval '7 day'
) as week
FROM doctor_profiles
) w LEFT JOIN
"doctor_profiles" dp
ON DATE_TRUNC('week', created_at) = w.week
GROUP BY w.week
ORDER BY w.week;