Question
Need help with changing the query which does pivot of counts of sections offered by department by selected years, but the years are explicitly selected
Need help with changing the query which does pivot of counts of sections offered by department by selected years, but the years are explicitly selected for display.
Want to tweak the query so that all the years are dynamically displayed, i.e. we do not need to explicitly select years to be displayed.
select dept_name,
(select count(s2.sec_id) from section s2 join course c2 on s2.course_id = c2.course_id
where c2.dept_name = course.dept_name
and s2.year in (2002, 2003, 2004, 2007, 2009, 2010)) "Count in that dept",
(select count(sec_id) from section) "Overall Count",
sum(if(year = 2002, 1,0)) as 'Year 2002',
sum(if(year = 2003, 1,0)) as 'Year 2003',
sum(if(year = 2004, 1,0)) as 'Year 2004',
sum(if(year = 2007, 1,0)) as 'Year 2007',
sum(if(year = 2009, 1,0)) as 'Year 2009',
sum(if(year = 2010, 1,0)) as 'Year 2010'
from section join course on section.course_id = course.course_id
group by dept_name
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started