得到传递关系的数量

时间:2014-12-23 09:52:30

标签: sql oracle11g

我有3张桌子

客户

  • cus_id

站点

  • sit_cus_id(来自cus的外键),
  • sit_id [一位客户有多个网站]

服务

  • ser_sit_id(来自坐的外键),
  • ser_id& nbsp [one site has multiple services]

我希望SQL查询将报告作为

cus_id | # of Sites | # of Services

1 个答案:

答案 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;