Oracle使用SQL語句可以實現日歷的功能,下面就為您詳細介紹Oracle使用SQL語句生成日歷的實現方法,希望對您能有所幫助。
1 要構造某年某月的日歷,必須先知道這個月的開始時間,結束時間及天數
開始日期 例如 2006年11月
- select to_date('20061101','yyyymmdd') as startDayOfMon from dual;
結束日期
- select last_day(to_date('20061101','yyyymmdd')) as endDayOfMon from dual;
日期區間天數
- select last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1 as DayOfMon
- from dual;
2 接下來就是需要得到開始時間到結束時間每一天的結果集
- select * from (
- select to_date('20061101','yyyymmdd') + level - 1 as everyDay from dual
- connect by level <=
- (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1));
3 再進一步則是將該月中的日期分解成第幾周,星期幾。
- select everyDay,to_char(everyday,'yyyy') as 年,
- to_char(everyday,'mm') as 月,
- to_char(everyday,'dd') as 日,
- to_char(everyday,'dy') as 星期幾,
- lpad(to_char(everyday,'w'),6) as 該月的第幾周,
- lpad(to_char(everyday,'ww'),6) as 該年的第幾周
- from(select to_date('20061101','yyyymmdd') + level - 1 as everyDay from dual
- connect by level <=
- (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1));
4 這個結果集求出來後,接下拉就是使用DECODE函數進行行列轉換了
- select everyDay,to_char(everyday,'yyyy') as 年,
- to_char(everyday,'mm') as 月,
- to_char(everyday,'dd') as 日,
- to_char(everyday,'dy') as 星期幾,
- lpad(to_char(everyday,'w'),6) as 該月的第幾周,
- lpad(to_char(everyday,'ww'),6) as 該年的第幾周,
- lpad(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd')),3) as 星期日,
- lpad(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd')),3) as 星期一,
- lpad(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd')),3) as 星期二,
- lpad(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd')),3) as 星期三,
- lpad(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd')),3) as 星期四,
- lpad(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd')),3) as 星期五,
- lpad(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd')),3) as 星期六
- from(select to_date('20061101','yyyymmdd') + level - 1 as everyDay from dual
- connect by level <=
- (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1));
5 再進一步就是統計匯總了,大家發現一個小問題沒有?
就是該月的第幾周這裡是按本月開始是星期幾為開始的日期,很有意思,
這樣我們按該日是該年的第幾周則是以今年開始日期是星期幾為開始日期
- select to_char(everyday,'w') as week,
- sum(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd'))) as 星期日,
- sum(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd'))) as 星期一,
- sum(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd'))) as 星期二,
- sum(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd'))) as 星期三,
- sum(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd'))) as 星期四,
- sum(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd'))) as 星期五,
- sum(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd'))) as 星期六
- from(select to_date('20061101','yyyymmdd') + level - 1 as everyDay
- from dual
- connect by level <= (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1)
- )
- group by to_char(everyday,'w');
6 以上日歷基本成功,但還有一個問題,就是一周的開始時間問題
- select to_char(everyday,'ww') as week,
- sum(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd'))) as 星期日,
- sum(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd'))) as 星期一,
- sum(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd'))) as 星期二,
- sum(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd'))) as 星期三,
- sum(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd'))) as 星期四,
- sum(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd'))) as 星期五,
- sum(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd'))) as 星期六
- from(select to_date('20061101','yyyymmdd') + level - 1 as everyDay
- from dual
- connect by level <= (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1)
- )
- group by to_char(everyday,'ww');
7 這樣雖然可以解決,但還存在問題,大家可以考慮下!也可以考慮下年歷怎麼做!
- select ceil((to_char(everyday,'dd')+(to_char(to_date('20061101','yyyymmdd'),'d')-1))/7) as week,
- sum(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd'))) as 星期日,
- sum(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd'))) as 星期一,
- sum(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd'))) as 星期二,
- sum(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd'))) as 星期三,
- sum(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd'))) as 星期四,
- sum(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd'))) as 星期五,
- sum(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd'))) as 星期六
- from(select to_date('20061101','yyyymmdd') + level - 1 as everyDay
- from dual
- connect by level <= (last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1)
- )
- group by ceil((to_char(everyday,'dd')+(to_char(to_date('20061101','yyyymmdd'),'d')-1))/7);
以上是最終的結果。