Where The Streets Have No Name

Pivot working days of a month 본문

Developement/DataBase

Pivot working days of a month

highheat 2008. 1. 25. 22:29
http://forums.oracle.com/forums/thread.jspa?threadID=609947

with month as (
  select trunc(sysdate,'mon')+rownum-1 any_dt
  from dual
  connect by level <= to_number(to_char(last_Day(sysdate),'dd'))
),
days as (
  select any_dt, to_char(any_dt,'DD') dy,
    ceil(( 7+(trunc(any_dt,'d')-trunc(any_dt,'mon')) )/7) wk, to_char(any_dt,'D') wd
  from month
)
select max(decode(wd,'1',dy,null)) d1,
  max(decode(wd,'2',dy,null)) d2,
  max(decode(wd,'3',dy,null)) d3,
  max(decode(wd,'4',dy,null)) d4,
  max(decode(wd,'5',dy,null)) d5,
  max(decode(wd,'6',dy,null)) d6,
  max(decode(wd,'7',dy,null)) d7
from days
group by wk
order by wk
/

D1 D2 D3 D4 D5 D6 D7
-- -- -- -- -- -- --
      01 02 03 04 05
06 07 08 09 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

5 rows selected.