我正在努力使用正确的SQL语法来返回一列中特定值的计数.
此查询有效(可能语法不正确,但SQL Server 2008似乎很高兴)
SELECT StudentID, count(UnApproved)as Late, count(Unapproved) as Absent from results
WHERE unapproved=1 and StudentID in
(
SELECT studentid FROM [Results]
WHERE StudentYearLevel='10' and Date > 20130101) group by StudentID
)
Run Code Online (Sandbox Code Playgroud)
当然,由于'where'的位置,两个Late和Absent列都返回相同的值.
所以这是做(从右边)确定"10年级"成员的学生的ID.
然后,对于返回的每个学生ID,我需要它返回未经批准的缺席类型记录的未批准缺勤计数,并且在下一列中,还返回类型为2的未批准缺勤计数.
如果我尝试像这样提交查询: -
SELECT StudentID, count(UnApproved)as Late where unapproved=2, count(Unapproved) as Absent from results
where unapproved=1 and StudentID in
(
SELECT studentid FROM [Results] where StudentYearLevel='10' and Date > 20130101
)
group by StudentID
Run Code Online (Sandbox Code Playgroud)
SQL Server破解了它,并以红色突出显示整个查询.
我需要最后这三列: -
StudentID | Late …