简体   繁体   中英

How to create a table with date by range

I am trying to get a result from oracle developer. I need to join 3 table with dates, the output should be :

Date        sum    count   num
2012-10-01  5000   30      15
2012-10-02  0      0       0
2012-10-03  60     150     350
2012-10-04  20     200     300
2012-10-05  1000   100     200
2012-10-06  1500   109     400

the input range is between 2012-10-01 and 2012-10-06. If it can't find data put the date with 0 in all other columns

Thanks in advanced.

You should generate all the dates and then left join the tables on dates.

Something like(I used two tables):

select date_col, nvl(sum(t1.val),0) + nvl(sum(t2.val,0)) as sum
from 
  (select to_date('01-OCT-2012','dd-mon-yyyy') + level - 1 as date_col
   from dual connect by level <= to_date('06-OCT-2099','dd-mon-yyyy') - to_date('01-OCT-2012','dd-mon-yyyy') + 1
   ) d
left join table1 t1 on (t1.date_column = d.date_col)
left join table2 t2 on (some_t1_t2_join_condition and t2.date_column = d.date_col);

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