oracle創建本月日曆

with x1 as

/*1、給定一個日期*/

(select to_date('2015-07-01','yyyy-mm-dd') as cur_date from dual),

x2 as

/*2、取月初*/

(select trunc(cur_date,'mm') as 月初,

            add_months(trunc(cur_date,'mm'),1) as 下月初 from x1),

x3 as

/*3、枚舉當月所有的天*/

(select 月初+(level-1) as 日 from x2

connect by level<=(下月初-月初)),

x4 as

/*4、提取周信息*/

(select to_char(日,'iw') 所在周,

            to_char(日,'dd') 日期,

            to_number(to_char(日,'d') ) 周幾 

from x3)

select max(case 周幾 when 2 then 日期 end) 週一,

          max(case 周幾 when 3 then 日期 end) 週二,

          max(case 周幾 when 4 then 日期 end) 週三,

          max(case 周幾 when 5 then 日期 end) 週四,

          max(case 周幾 when 6 then 日期 end) 週五,

          max(case 周幾 when 7 then 日期 end) 週六,

          max(case 周幾 when 1 then 日期 end) 週日

from x4

group by 所在周

order by 所在周;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章