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'の場合にのみ取得します。このクエリを書き直して、m.NRC_CLASS列に行がない場合に「0」としてカウントできるようにする方法'D'値を含む

4

3 に答える 3

1

これを実現するには、左結合を使用する必要があります

あなたがこれを試すことができるように

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 に答える