I have two SELECT statements that I'd like to sum. Both queries work fine but I'm not able to SUM the output of total. I tried following this question but unable to SUM by wrapping the query in select id, sum(amount) from ( )
SELECT "patient_profiles"."id", count(distinct recommendations.id) AS total
FROM "patient_profiles"
LEFT OUTER JOIN
"recommendations" ON "recommendations"."patient_profile_id" = "patient_profiles"."id"
GROUP BY "patient_profiles"."id"
UNION
SELECT "patient_profiles"."id", count(distinct patient_profile_potential_doctors.id) AS total
FROM "patient_profiles"
LEFT OUTER JOIN "patient_profile_potential_doctors" ON "patient_profile_potential_doctors"."patient_profile_id" = "patient_profiles"."id"
GROUP BY "patient_profiles"."id"
Select ID, sum(Total) from
(
SELECT "patient_profiles"."id" [ID], count(distinct recommendations.id) AS total
FROM "patient_profiles"
LEFT OUTER JOIN
"recommendations" ON "recommendations"."patient_profile_id" = "patient_profiles"."id"
GROUP BY "patient_profiles"."id"
UNION
SELECT "patient_profiles"."id" [ID], count(distinct patient_profile_potential_doctors.id) AS total
FROM "patient_profiles"
LEFT OUTER JOIN "patient_profile_potential_doctors" ON "patient_profile_potential_doctors"."patient_profile_id" = "patient_profiles"."id"
GROUP BY "patient_profiles"."id"
) x
group by ID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With