简体   繁体   中英

Expand table rows based on column value

I have a table with two columns; EVENT_DATE date and RANG number 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

| 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 |

Here you go.

select to_char(event_date + (l - 1),'dd/mm/yyyy') from tab1 t
left outer join (
  select level l from dual
  connect by level <= (select max(rang) + 1 from tab1)
) on l <= rang + 1
order by event_date, 1;

SQL Fiddle

This should work:

select (t.event_date + t2.value) as event_date
from t, (select rownum -1 as value from all_objects) t2
where t2.value <= t.rang
order by 1 asc;

Another possible answer. Assume Event1 is your table

with tmp as
(select MAX(RANG) s FROM Event1)
,rec as (
    select 0 num
    union all
    select num+1 from rec where num < (select s from tmp))
SELECT DATEADD(DAY, r.num, e.Event_Date) Result from rec r join Event1 e on r.num <= e.RANG
ORDER BY DATEADD(DAY, r.num, e.Event_Date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM