mysql - sql query help on multiple count columns and group by -


i have following table students:

id | status | school | name ---------------------------- 0  | fail   | skool1 | dan 1  | fail   | skool1 | steve 2  | pass   | skool2 | joe 3  | fail   | skool2 | aaron 

i want result gives me

school | fail | pass   --------------------- skool1 | 2    | 0    skool2 | 1    | 1     

i have it's slow,

select s.school, (  select count( * )  school name = s.name , status = 'fail' ) fail, (  select count( * )  school name = s.name , status = 'pass' ) pass,  students s group s.school 

suggestions?

something should work:

select      school,     sum(case when status = 'fail' 1 else 0 end) [fail],     sum(case when status = 'pass' 1 else 0 end) [pass] students group school order school 

edit
forgot, write query way:

select      school,     count(case when status = 'fail' 1 end) [fail],     count(case when status = 'pass' 1 end) [pass] students group school order school 

i'm not sure if there's performance benefit second query. guess if there it's small. tend use first query because think it's more clear both should work. also, don't have mysql instance handy test with, according @johan order clauses unnecessary.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -