schools: income_proxies_by_year_for_state_and_grade

Custom SQL query returning 3 rows (hide)

with combined as (
select
  'homeless_int' as column,
  sum(homeless_int) filter (where year = 2015) as total_2015,
  sum(homeless_int) filter (where year = 2016) as total_2016,
  sum(homeless_int) filter (where year = 2017) as total_2017,
  sum(homeless_int) filter (where year = 2018) as total_2018,
  sum(homeless_int) filter (where year = 2019) as total_2019,
  sum(homeless_int) filter (where year = 2020) as total_2020,
  sum(homeless_int) filter (where year = 2021) as total_2021,
  (
    sum(homeless_int) filter (where year = 2021)) - (sum(homeless_int) filter (where year = 2020)
  ) as change_2020_2021,
  round(
    (1.0 * (
      sum(homeless_int) filter (where year = 2021)) - (sum(homeless_int) filter (where year = 2020))) /
      sum(homeless_int) filter (where year = 2020) * 100,
  2) as percent_change
from enrollments
where state = :state
and grade = :grade
and term = :term
union
select
  'free_and_reduced_int' as column,
  sum(free_and_reduced_int) filter (where year = 2015) as total_2015,
  sum(free_and_reduced_int) filter (where year = 2016) as total_2016,
  sum(free_and_reduced_int) filter (where year = 2017) as total_2017,
  sum(free_and_reduced_int) filter (where year = 2018) as total_2018,
  sum(free_and_reduced_int) filter (where year = 2019) as total_2019,
  sum(free_and_reduced_int) filter (where year = 2020) as total_2020,
  sum(free_and_reduced_int) filter (where year = 2021) as total_2021,
  (
    sum(free_and_reduced_int) filter (where year = 2021)) - (sum(free_and_reduced_int) filter (where year = 2020)
  ) as change_2020_2021,
  round(
    (1.0 * (
      sum(free_and_reduced_int) filter (where year = 2021)) - (sum(free_and_reduced_int) filter (where year = 2020))) /
      sum(free_and_reduced_int) filter (where year = 2020) * 100,
  2) as percent_change
from enrollments
where state = :state
and grade = :grade
and term = :term
union
select
  'low_income_int' as column,
  sum(low_income_int) filter (where year = 2015) as total_2015,
  sum(low_income_int) filter (where year = 2016) as total_2016,
  sum(low_income_int) filter (where year = 2017) as total_2017,
  sum(low_income_int) filter (where year = 2018) as total_2018,
  sum(low_income_int) filter (where year = 2019) as total_2019,
  sum(low_income_int) filter (where year = 2020) as total_2020,
  sum(low_income_int) filter (where year = 2021) as total_2021,
  (
    sum(low_income_int) filter (where year = 2021)) - (sum(low_income_int) filter (where year = 2020)
  ) as change_2020_2021,
  round(
    (1.0 * (
      sum(low_income_int) filter (where year = 2021)) - (sum(low_income_int) filter (where year = 2020))) /
      sum(low_income_int) filter (where year = 2020) * 100,
  2) as percent_change
from enrollments
where state = :state
and grade = :grade
and term = :term) select * from combined order by percent_change

Query parameters

Edit SQL

columntotal_2015total_2016total_2017total_2018total_2019total_2020total_2021change_2020_2021percent_change
free_and_reduced_int                  
homeless_int                  
low_income_int