postgresql - How would I go about writing the SQL for the attached report? -
we're moving data system another. i'm having rewrite reports new system has custom reporting module can write query , paste window , push out end user. have 1 report of users have been requesting can't figure out how write it.
it breaks down school population grade level, gender , race. see attached below.
i wrote breaks down population grade level not sure go here.
select gl.title grade, count (s.student_id) students s, student_enrollment se, school_gradelevels gl s.student_id = se.student_id , se.school_id=gl.school_id , se.grade_id=gl."id" , se.syear =2012 , se.end_date null , se.school_id =10 group gl.title order grade;
for reference gender s.gender , race s.race. i'm wondering if going have purchase reporting software crystal reports.
there no data example presum gender , race information captured against student.
what want initial table that:
gradelevel | category | numofstudents ------------------------------------------------ pk | b - male | 10 pk | b - female | 12 0 | b - male | 5 0 | b - female | 6
this easy achieve doing that:
select gl.title gradelevel, s.race + '-' +s.gender category count (s.student_id) numofstudents students s, inner join student_enrollment se on s.student_id = se.student_id, inner join school_gradelevels gl on se.school_id=gl.school_id , se.grade_id=gl."id" se.syear =2012 , se.end_date null , se.school_id =10 group gl.title, s.race + '-' +s.gender order grade;
next want make pivot out of data using reporting tool weather ssrs or crystal or else supports matrix reports.
if there no report tool available knows how make matrix use sql pivot
clause: http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
select gradelevel, [b-male], [b-female] .... ( select gl.title gradelevel, s.race + '-' +s.gender category count (s.student_id) numofstudents students s, inner join student_enrollment se on s.student_id = se.student_id, inner join school_gradelevels gl on se.school_id=gl.school_id , se.grade_id=gl."id" se.syear =2012 , se.end_date null , se.school_id =10 group gl.title, s.race + '-' +s.gender ) rawdata pivot ( sum(numofstudents) category in ( [b-male], [b-female], ... ) ) pvt
Comments
Post a Comment