如何仅对邀请表使用子查询?我希望来自patient_profiles
的所有记录以及邀请加入时仅使用在特定日期之后创建的记录?
SELECT p.first_name,
COUNT(invites.invited_by_id)as invite_count
FROM patient_profiles AS p
LEFT OUTER JOIN patient_profiles AS invites ON invites.invited_by_id = p.id
WHERE p.is_test = false AND AND invites.created_at >= '2017-10-16'::date
GROUP BY p.first_name
答案 0 :(得分:1)
您不需要子查询。只需将日期条件移至ON
子句:
SELECT p.first_name,
COUNT(invites.invited_by_id)as invite_count
FROM patient_profiles p LEFT OUTER JOIN
patient_profiles invite
ON invites.invited_by_id = p.id AND invites.created_at >= '2017-10-16'::date
WHERE p.is_test = false
GROUP BY p.first_name;
答案 1 :(得分:0)
您可以使用HAVING
。
SELECT p.first_name, COUNT(invites.invited_by_id)as invite_count
FROM patient_profiles p
GROUP BY p.first_name
HAVING p.is_test = false AND p.created_at >= '2017-10-16'::date