我有3张桌子
客户
站点
服务
我希望SQL查询将报告作为
cus_id | # of Sites | # of Services
答案 0 :(得分:0)
试试这个:
SELECT C.cus_id,
COUNT(DISTINCT S.sit_id) NoOfSites,
COUNT(DISTINCT SS.ser_sit_id) NoOfServices
FROM Customer C
LEFT OUTER JOIN Sites S ON C.cus_id = S.sit_cus_id
LEFT OUTER JOIN Services SS ON S.sit_id = SS.ser_sit_id
GROUP BY C.cus_id;