1

我的查询是

select m.year_id,Count(*) as Distinctions from NewTestDB.dbo.[Master_MARKS2005] as m
join NewTestDB.dbo.Master_Student as s
on s.Student_id=m.Student_Id
join NewTestDB.dbo.Master_School as sc
on sc.school_id=s.school_code
where sc.SCHOOL_CODE= 'an0001' and m.YEAR_ID between 1 and 8 and m.[NRC_CLASS]='D'
group by m.year_id,

我想要 1 到 8 之间的所有 year_id 但现在我只在 m.[NRC_CLASS]='D'包含“D”值

4

3 回答 3

1

您应该使用 Left Join 来实现这一点

就像你可以试试这个

SELECT m.year_id,COUNT(*) AS Distinctions 
       FROM NewTestDB.dbo.[Master_MARKS2005] AS m
LEFT JOIN 
       NewTestDB.dbo.Master_Student AS s
ON 
       s.Student_id=m.Student_Id
JOIN 
       NewTestDB.dbo.Master_School AS sc
ON 
       sc.school_id=s.school_code
WHERE 
       sc.SCHOOL_CODE= 'an0001' 
       AND 
       m.YEAR_ID BETWEEN 1 AND 8 
       AND 
       m.[NRC_CLASS]='D' 
GROUP BY m.year_id,
于 2012-09-21T07:45:18.793 回答
1

尝试这个:

select  m.year_id,
        Count(CASE WHEN m.[NRC_CLASS]='D' THEN 1 ELSE 0 END) as Distinctions
from    NewTestDB.dbo.[Master_MARKS2005] as m
join    NewTestDB.dbo.Master_Student as s
on      s.Student_id=m.Student_Id
join    NewTestDB.dbo.Master_School as sc
on      sc.school_id=s.school_code
where   sc.SCHOOL_CODE= 'an0001' and m.YEAR_ID between 1 and 8
group   by m.year_id,
于 2012-09-21T07:47:29.310 回答
1
select isnull(m.year_id,''),Count(*) as Distinctions from NewTestDB.dbo.[Master_MARKS2005] as m
left join NewTestDB.dbo.Master_Student as s
on s.Student_id=m.Student_Id
left join NewTestDB.dbo.Master_School as sc
on sc.school_id=s.school_code
where sc.SCHOOL_CODE= 'an0001' and m.YEAR_ID between 1 and 8 and m.[NRC_CLASS]='D'
group by m.year_id
于 2012-09-21T07:49:18.037 回答