1

我有一个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'

但我将如何获得上述结果的年度数据

4

2 回答 2

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 
于 2012-08-03T08:29:10.293 回答
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
于 2012-08-03T08:26:33.700 回答