简体   繁体   中英

Oracle join dual daterange

I'm trying to place data into a date range from a dual query. The outcome should look like this:

SINGLEDAY       FLAG
01-NOV-2016     1
02-NOV-2016     -
03-NOV-2016     -
04-NOV-2016     1
05-NOV-2016     -

For the list of days I'm using

select (to_date('11.2016','mm.yyyy')+level-1) as SINGLEDAY
from dual
connect by level <= TO_CHAR(LAST_DAY(to_date('11.2016','mm.yyyy')),'DD')

But how do I join content of my table xyz with the outcome? table xyz should be connected by a DATE_OF_SERVICE (date)column. Any help would be highly appreciated.

This is basically a left join on your query. Assuming at most one row in your table for each date:

with d as (
      select (to_date('09.2016','mm.yyyy')+level-1) as singleday
      from dual
      connect by level <= TO_CHAR(LAST_DAY(to_date('09.2016', 'mm.yyyy')), 'DD')
     )
select t.singleday,
       (case when t.date_of_service is not null then 1 end) as flag
from d left join
     t
     on d.singleday = t.date_of_service
order by d.singleday;

If there are multiples, then perhaps you what group by :

with d as (
      select (to_date('09.2016','mm.yyyy')+level-1) as singleday
      from dual
      connect by level <= TO_CHAR(LAST_DAY(to_date('09.2016', 'mm.yyyy')), 'DD')
     )
select t.singleday, count(t.date_of_service) as cnt
from d left join
     t
     on d.singleday = t.date_of_service
group by d.singleday
order by d.singleday;

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