2012-08-03 27 views
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' 

除H嗷嗷我會得到上述結果一年明智數據

+0

集團通過一年?這似乎是學生ID的最後四個字符? – 2012-08-03 08:18:35

回答

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 
+0

謝謝@Joe但stdID列也包含其他教職員工以及有像203BEX2009,203BEL2009 ..等數據..我需要的數據只有BCT.What做在這種情況下 – user1500383 2012-08-06 04:56:00

+0

@ user1500383:我已更新我的查詢PLZ檢查現在 – 2012-08-06 05:45:33

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