SQL-为分组返回0值

时间:2019-03-18 16:32:42

标签: sql tsql

我有3个表,Member_School,其中包含成员与学校之间的链接,Schools,其中包含学校信息,Category,这是用于完整类别名称(成员类别)的简单查找表。

我正在尝试获取每所学校的所有成员类别的计数,但是无法获得特定学校中没有成员的类别的0计数。到目前为止,这是我的查询:

WITH MemberNums AS
(
SELECT School_No, Category, COUNT(Member_No) AS NumberOfMembs

FROM Member_School

WHERE Finish_Date > GETDATE()

AND Category IS NOT NULL

GROUP BY School_No, Category

)


SELECT School.School_No, Category.Category, MemberNums.NumberOfMembs AS NumberOfMembs, School.School_Name, School.ATH_No, School.AWA_No, School.CLE_No, School.CYC_No, School.CYF_No, School.OBS_No, School.PEN_No, School.RHI_No, 
School.STA_No, School.SYL_No

FROM School 

INNER JOIN MemberNums 
ON School.School_No = MemberNums.School_No

LEFT JOIN Category
ON Category.Category_No = MemberNums.Category

ORDER BY School_No

例如,此查询可能为ID为123的学校返回8行,但“类别”表中有10个类别。我想要得到的是每所学校10行,并计算每个类别中的成员人数(包括在Member_School表中没有特定类别记录的学校的零计数)

这是每个表中的示例行:

类别:

Category_No: Cat1
Category: Category1

学校:

School_No: 123
School_Name: Highschool
Memb_No: 10
Secondary: True
Job_No: XJI314
ATH_No: 1
AWA_No: 1
CLE_No: 1
CYC_No: 1
CYF_No: 1
OBS_No: 1
PEN_No: 1
RHI_No: 1
STA_No: 1
SYL_No: 1

会员学校:

Rec_No: 1
Member_No: 2
School_No: 123
Start_Date: 2018-03-19 00:00:00.000
End_Date: 2020-03-19 00:00:00.000
Category: Cat1

3 个答案:

答案 0 :(得分:2)

如果需要零,请使用cross join生成行,然后引入现有数据:

select s.School_No, c.Category, count(ms.School_No) as NumberOfMembs as NumberOfMembs,
       s.School_Name, s.ATH_No, s.AWA_No, School.CLE_No,
       s.CYC_No, s.CYF_No, s.OBS_No, s.PEN_No, s.RHI_No, 
       s.STA_No, s.SYL_No
from school s cross join
     categories c left join
     Member_School ms
     on ms.School_No = s.School_No and
        ms.Category = c.Category_No and
        ms.Finish_Date > getdate()
group by s.School_No, c.Category, 
         s.School_Name, s.ATH_No, s.AWA_No, School.CLE_No,
         s.CYC_No, s.CYF_No, s.OBS_No, s.PEN_No, s.RHI_No, 
         s.STA_No, s.SYL_No;

答案 1 :(得分:0)

使用左联接

WITH MemberNums AS
(
SELECT School_No, Category, COUNT(Member_No) AS NumberOfMembs

FROM Member_School

WHERE Finish_Date > GETDATE()

AND Category IS NOT NULL

GROUP BY School_No, Category

)    

SELECT School.School_No, Category.Category, MemberNums.NumberOfMembs AS NumberOfMembs, School.School_Name, School.ATH_No, School.AWA_No, School.CLE_No, School.CYC_No, School.CYF_No, School.OBS_No, School.PEN_No, School.RHI_No, 
School.STA_No, School.SYL_No

FROM School 

left JOIN MemberNums 
ON School.School_No = MemberNums.School_No

LEFT JOIN Category
ON Category.Category_No = MemberNums.Category

ORDER BY School_No

答案 2 :(得分:0)

您是否尝试过使用正确的外部联接?使用正确的外部联接和合并功能以确保显示0而不是null。 我在下方发布了一个示例查询

WITH MemberNums AS
(
SELECT School_No, Category, COUNT(Member_No) AS NumberOfMembs

FROM Member_School

WHERE Finish_Date > GETDATE()

AND Category IS NOT NULL

GROUP BY School_No, Category

)


SELECT School.School_No, Category.Category, COALESCE(MemberNums.NumberOfMembs, 0) AS NumberOfMembs, School.School_Name, School.ATH_No, School.AWA_No, School.CLE_No, School.CYC_No, School.CYF_No, School.OBS_No, School.PEN_No, School.RHI_No, 
School.STA_No, School.SYL_No

FROM School 

INNER JOIN MemberNums 
ON School.School_No = MemberNums.School_No

RIGHT OUTER JOIN Category
ON Category.Category_No = MemberNums.Category

ORDER BY School_No

如果您希望来自school和member_school的所有结果都可能没有类别,那么您可能要考虑使用FULL OUTER JOIN