I’m try to get the output of How many times each location is used for clus meetings, trainings, and competitions for my database. I figured out how to do each separately but am having trouble combining them into one output table.
SELECT l.LocId, l.Postcode, count(c.CId) FROM locations l left join competitions c
on l.LocId = c.LocId
GROUP BY LocId;
SELECT l.LocId, l.Postcode, count(t.TId) FROM locations l left join trainings t
on l.LocId = t.LocId
GROUP BY LocId;
SELECT l.LocId, l.Postcode, count(cl.Cno) FROM locations l left join clubs cl
on l.LocId = cl.LocId
GROUP BY LocId;
I’ve tried combining them using left join and can’t find an other ways to try
2
Answers
Assuming that
LocId
,CId
,TId
andCno
are the primary keys of the tables, do aLEFT
join oflocations
to all 3 tables and aggregate withCOUNT(DISTINCT ...)
:Just use subqueries in the SELECT clause: