Generate a sequence of date from arrival_dt till departure_dt using Oracle SQL -
i need oracle sql logic generate sequence of dates starting arrival_dt till depature_dt please find current data set
prop_id blk_id arrival_dt departure_dt num_stay start_dt day_num star_num booking_num create_dt room_type revenue room_num adults 353 3669281 18-jun-15 21-jun-15 1 18-jun-15 1 579071315 21803 13-jul-15 w 314 2009 1 353 3669281 18-jun-15 21-jun-15 1 19-jun-15 2 579071315 21803 13-jul-15 w 314 2009 1 353 3669281 18-jun-15 21-jun-15 1 19-jun-15 3 579071315 21803 13-jul-15 w 314 2009 1
i want start_dt column having values 18-jun-15, 19-jun-15 , 20-jun-15
desired output
prop_id blk_id arrival_dt departure_dt num_stay start_dt day_num star_num booking_num create_dt room_type revenue room_num adults 353 3669281 18-jun-15 21-jun-15 1 18-jun-15 1 579071315 21803 13-jul-15 w 314 2009 1 353 3669281 18-jun-15 21-jun-15 1 19-jun-15 2 579071315 21803 13-jul-15 w 314 2009 1 353 3669281 18-jun-15 21-jun-15 1 20-jun-15 3 579071315 21803 13-jul-15 w 314 2009 1
the query i'm using complex , using multiple tables fetch data , confuse more, need way given arrival_dt , departure_dt, should generate sequence of dates within arrival_dt , departure_dt range can integrate query i'm using it.
in above mentioned example, arrival_dt 18-jun-2015 , departure_dt 21-jun-2015 , want start_dt in sequence starting arrival_dt till departure_dt 18-jun-2015, 19-jun-2015 , 20-jun-2015
for dataset day_num
column appears sequence starting 1, can use expression: arrival_dt + day_num - 1 new_date
calculate sequence of dates:
select t.*, arrival_dt + day_num - 1 new_date table1 t
demo: http://sqlfiddle.com/#!4/c0c33/1
generally following query can used generate sequence of values 1 x:
select level sequence dual connect level <= x
for example if want generate numbers 1 15:
select level sequence dual connect level <= 15
or when sequence -5, -4, -3, ...., 3, 4, 5 needed:
select level - 6 sequence dual connect level <= 5 - ( -6 ) ;
if want create sequence of dates 1 date another, calculate number of days between these 2 dates - substract these 2 dates. example when start date is: 2015-05-01 , end date 2015-05-05, query is:
select level + to_date('2015-05-01','yyyy-mm-dd') - 1 new_date dual connect level <= 1 + to_date('2015-05-05','yyyy-mm-dd') - to_date('2015-05-01','yyyy-mm-dd')
Comments
Post a Comment