我有学生记录,他们每天都进行驾驶考试=,我想计算今天通过并且未通过驾驶考试的学生总人数,例如
StudentName Status date pass fail
test1 something 2014-05-14 1 0
test2 something 2014-05-14 0 1
test1 something 2014-04-14 0 1
test1 something 2014-03-14 0 1
目前我有这个查询 - select count(case pass where 1 then 1 else null end) As TotalPassed, count(case pass where 1 then 1 else null end) as Totalfailed from DatabaseName where date>='2014-05-14'
问题是此查询仅搜索2014-05-14的总记录,但我想要的是特定学生的记录总数。对于此查询,我得到结果为;
StudentName Status date pass fail
test1 Something 2014-05-14 1 0
test2 something 2014-05-14 0 1
但我需要的是查询计算每个人通过或未通过考试的总时间,因此预期结果应为:
StudentName Status date pass fail
test1 something 2014-05-14 1 2
test2 something 2014-05-14 0 1
答案 0 :(得分:3)
您应该使用SUM
代替COUNT
,最后添加GROUP BY
。
SELECT StudentName,
MAX(date) as date,
SUM(pass) As TotalPassed,
SUM(fail) As Totalfailed,
SUM(pass) + SUM(fail) as TotalTriesVersion1,
COUNT(*) as TotalTriesVersion2
FROM Table1
GROUP BY StudentName
here是sqlfiddle(下次请自己提供:))
奖励:我给了你两种方法来计算每个学生参加考试的总时间,正如我从你想要的那样的评论中所理解的那样。
答案 1 :(得分:0)
你需要像这样使用GROUP BY子句:
SELECT COUNT(CASE pass WHERE 1 THEN 1 ELSE NULL END) AS TotalPassed,
COUNT(CASE pass WHERE 1 THEN 1 ELSE NULL END) AS Totalfailed FROM DatabaseName
WHERE date>='2014-05-14' GROUP BY StudentName
答案 2 :(得分:0)
select studentname , date , sum(pass) as Totalpass, sum(fail) as TotalFail from DatabaseName where date ='2014-05-14'
group by studentname,date
答案 3 :(得分:0)
通过
小组尝试从表中选择StudentName,date,sumpass,fail,count(*) 按学生姓名分组,日期,通过,失败