I have a table in the following format e.g.
ID DATE MEASURE
-- ---- -------
1 30-JAN 10
1 02-FEB 15
1 12-FEB 20
1 14-FEB 30
1 15-FEB 35
Each day, this has to be converted into a summary table with columns:
ID
MEASURE
DAYS_AGO_INDICATOR
where DAYS_AGO_INDICATOR = CURRENT, 1_DAY_AGO, 7_DAYS_AGO, 14_DAYS_AGO and possibly loads of others up to a year ago. These are all offsets from the current date.
In the above example (with 15-jan as the current date), the values would be:
ID ME IND
-- -- --
1 35 CUR
1 30 1DAY
1 15 7DAY
1 10 14DAY
The only way I can think of doing this is with a union query, something like:
select * from
(select id, 'CUR',
row_number() over
(partition by ID order by datecol desc) as rown
from table
where datecol <= current date )
where rown = 1
union all
select * from
(select id, '1DAY',
row_number() over
(partition by ID order by datecol desc) as rown
from table
where datecol <= current date - 1 day)
where rown = 1
...
Does anyone have any other ideas ?
ID DATE MEASURE
-- ---- -------
1 30-JAN 10
1 02-FEB 15
1 12-FEB 20
1 14-FEB 30
1 15-FEB 35
Each day, this has to be converted into a summary table with columns:
ID
MEASURE
DAYS_AGO_INDICATOR
where DAYS_AGO_INDICATOR = CURRENT, 1_DAY_AGO, 7_DAYS_AGO, 14_DAYS_AGO and possibly loads of others up to a year ago. These are all offsets from the current date.
In the above example (with 15-jan as the current date), the values would be:
ID ME IND
-- -- --
1 35 CUR
1 30 1DAY
1 15 7DAY
1 10 14DAY
The only way I can think of doing this is with a union query, something like:
select * from
(select id, 'CUR',
row_number() over
(partition by ID order by datecol desc) as rown
from table
where datecol <= current date )
where rown = 1
union all
select * from
(select id, '1DAY',
row_number() over
(partition by ID order by datecol desc) as rown
from table
where datecol <= current date - 1 day)
where rown = 1
...
Does anyone have any other ideas ?