I want to be able to calculate different age ranges across different as of dates over the course of a year without having to union all.
The query now would be something like this:
SELECT
to_char(last_day(add_months(sysdate,-1)),'YYYYMM') yrmnth,
FLOOR(months_between(last_day(add_months(sysdate,-1)),birth_date)/12),
count(distinct id) members
from agefile
WHERE to_char(from_date, 'YYYYMM')<=to_char(last_day(add_months(sysdate,-1)),'YYYYMM')
and to_char(thru_date, 'YYYYMM')>=to_char(last_day(add_months(sysdate,-1)),'YYYYMM')
GROUP BY to_char(last_day(add_months(sysdate,-1)),'YYYYMM'),
FLOOR(months_between(last_day(add_months(sysdate,-1)),birth_date)/12)
UNION ALL
SELECT
to_char(last_day(add_months(sysdate,-2)),'YYYYMM') yrmnth,
FLOOR(months_between(last_day(add_months(sysdate,-2)),birth_date)/12),
count(distinct id) members
from agefile
WHERE to_char(from_date, 'YYYYMM')<=to_char(last_day(add_months(sysdate,-2)),'YYYYMM')
and to_char(thru_date, 'YYYYMM')>=to_char(last_day(add_months(sysdate,-2)),'YYYYMM')
GROUP BY to_char(last_day(add_months(sysdate,-2)),'YYYYMM'),
FLOOR(months_between(last_day(add_months(sysdate,-2)),birth_date)/12)
UNION ALL
and so on...
so, instead of passing the agefile 12 times, I want to pass it once, but output 12X number of ages.
Example:
yrmnth age members 201809 1 100 201809 2 120 201809 3 145 201808 1 56
"How many members were age x as of the last day of every month for the last 12 months" would be the business question
Any way to get around passing the file 12 times and doing this with one select?
thank you
{ }button