patternMinor
Expand table rows based on column value
Viewed 0 times
expandrowscolumnvaluebasedtable
Problem
I have a table with two columns;
the first column holds a date for an event while the second column is for the period of that event. here is a sample of data
is there any way to expand the
EVENT_DATE date and RANG numberthe first column holds a date for an event while the second column is for the period of that event. here is a sample of data
| EVENT_DATE | RANG |
|------------|------|
| 03/01/2015 | 1 |
| 09/04/2015 | 3 |
| 15/10/2015 | 2 |is there any way to expand the
EVENT_DATE by increment it based on the RANG value, so the output will be like, | EVENT_DATE |
|------------|
| 03/01/2015 |
| 04/01/2015 |
| 09/04/2015 |
| 10/04/2015 |
| 11/04/2015 |
| 12/04/2015 |
| 15/10/2015 |
| 16/10/2015 |
| 17/10/2015 |Solution
You could use a CTE to get a table with a sequence of numbers up to the highest rank in your table, and then join with that:
with nums as (
select
level-1 lvl
from dual
connect by
level-1 < = (select max(rang) from foo)
)
select
foo.event_date + nums.lvl
, rang
from foo
join nums
on nums.lvl <= foo.rang
order by
foo.event_date + nums.lvl;Code Snippets
with nums as (
select
level-1 lvl
from dual
connect by
level-1 < = (select max(rang) from foo)
)
select
foo.event_date + nums.lvl
, rang
from foo
join nums
on nums.lvl <= foo.rang
order by
foo.event_date + nums.lvl;Context
StackExchange Database Administrators Q#115019, answer score: 2
Revisions (0)
No revisions yet.