目前我有2张桌子。服务1的名称和服务信息。
我需要从服务名称中获取所有唯一名称,并显示其中附加了多少服务信息。
SELECT cs.serviceDesc as 'SERVICE', COUNT(cr.serviceId) as 'Count',
FROM consultationRequests cr
JOIN consultationServices cs on cs.serviceId = cr.serviceId
JOIN demographic d on d.demographic_no = cr.demographicNo
WHERE cs.active = 1
AND cr.referalDate between '2018-11-31' and '2019-01-20'
GROUP BY cr.serviceId
咨询请求中还有另一列,其状态编号在1-4之间。我需要为每个状态添加另一列,并在服务名称内计算该状态内的信息数量。
答案 0 :(得分:0)
您可以使用条件聚合:
SELECT cs.serviceDesc as SERVICE,
COUNT(cr.serviceId) as Count,
SUM(cr.consultationRequests = 1) as cnt_1,
SUM(cr.consultationRequests = 2) as cnt_2,
SUM(cr.consultationRequests = 3) as cnt_3,
SUM(cr.consultationRequests = 4) as cnt_4
FROM consultationRequests cr JOIN
consultationServices cs
ON cs.serviceId = cr.serviceId JOIN
demographic d
ON d.demographic_no = cr.demographicNo
WHERE cs.active = 1 AND
cr.referalDate between '2018-11-31' and '2019-01-20'
GROUP BY cr.serviceId