I have a bunch of student records in the table.
school stduent_id start_date end_date
111 123 2010-02-03 2012-02-03
222 345 2013-02-03 2014-02-03
222 567 2015-02-03 2018-02-03
I wanted to count how many students each school has each year, say from 2014 all the way to 2021.
Currently, I am using lateral view, but it is hardcoded:
SELECT school
, yrs
, COUNT(DISTINCT stduent_id) AS cnt
FROM TABLE
LATERAL VIEW EXPLODE(ARRAY(2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021))a AS yrs
WHERE 1=1
AND YEAR(start_date) <= yrs
AND YEAR(end_date) >= yrs
I wanted this to be updated automatically when the year moves, eg, now should be 2014-2021, next year it should be 2015-2022.
Any idea how should I do this? Thank you!