我有一个StudentId(int),Subject(varchar(50)),Marks(int),IsPass(int),ExamDate(Datetime)
对于不同日期的特定学生,表格可以为同一主题提供多条记录。
我写了以下查询:
select StudentId, Count(IsPass)
from ExamEntry
where IsPass =1 group by StudentId
但不希望查询中的Where条件:
这可能是这样的:
Select StudentId, case when IsPass = 1 then count(IsPass) end
from ExamEntry
group by studentId
但是它显示了针对特定studentId的一条记录
我如何实现目标?
答案 0 :(得分:2)
答案是如此简单,没有人看到它:-)
SELECT StudentId, SUM(IsPass)
FROM ExamEntry GROUP BY StudentId
答案 1 :(得分:1)
SELECT StudentId,
COUNT(CASE WHEN IsPass = 1 THEN 'X' END) AS NumberOfPasses
FROM ExamEntry
GROUP BY StudentId
COUNT
仅计算NOT NULL
个值,CASE
具有隐式ELSE NULL
答案 2 :(得分:1)
也许你需要这样的东西?
SELECT StudentId, SUM(CASE WHEN IsPass = 1 THEN 1 ELSE 0 END) AS Number_Of_Passed_Exams
FROM ExamEntry
GROUP BY StudentId
答案 3 :(得分:0)
如果你不想使用where子句,那么Having子句是最简单的替代。
试试这个。
select StudentId, count(IsPass)AS PassedCount
from ExamEntry
group by StudentId,IsPass
having IsPass= 1