我有一个包含以下数据的表tbl_marksObtained
:
stdID sub marks_Obtained
201bct2007 computer 45
201bct2007 Drawing 0
202bct2007 computer 0
203bct2007 Drawing 65
.
230bct2007 computer 77
301bct2008 physics 0
301bct2008 computer 55
.
.
401bct2009 and so on..
现在我想将结果显示为:
year sub pass fail
2007 computer 20 10
2007 Drawing 15 15
2008 computer 28 2
2009 computer 20 10
我通过以下代码获得了特定年份的通过失败和主题:
SELECT
SUM( CASE WHEN marks_obtained > '0' THEN 1 END) AS pass,
SUM( CASE WHEN marks_obtained = '0' THEN 1 END) AS fail
FROM
tblstudentexammarks
WHERE
sub ='computer' AND stdID LIKE '%bce2007'
但我如何获得上述结果年度数据
答案 0 :(得分:0)
试试这个:
select stdID [Year],sub,
sum(case when PF='P' then 1 else 0 end) as Pass,
sum(case when PF='F' then 1 else 0 end) as Fail
from(
select RIGHT(stdID,4) stdID,sub,
case when marks_Obtained=0 then 'F' else 'P' end PF
from mark_sheet)a
WHERE
sub ='computer' AND stdID LIKE '%bce2007'
group by stdID,sub
答案 1 :(得分:0)
select
RIGHT(stdID,4) as year,
sub,
sum(CASE WHEN marks_Obtained>0 then 1 else 0 end) as pass,
sum(CASE WHEN marks_Obtained=0 then 1 else 0 end) as fail
from tblstudentexammarks
group by RIGHT(stdID,4),sub
order by 1