今天小麦苗给大家分享的是Oracle用SQL打印日历。
Oracle用SQL打印日历1.1 打印当月日历
SELECT MAX(DECODE(DOW, 1, D, NULL)) SUN,
MAX(DECODE(DOW, 2, D, NULL)) MON,
MAX(DECODE(DOW, 3, D, NULL)) TUE,
MAX(DECODE(DOW, 4, D, NULL)) WED,
MAX(DECODE(DOW, 5, D, NULL)) THU,
MAX(DECODE(DOW, 6, D, NULL)) FRI,
MAX(DECODE(DOW, 7, D, NULL)) SAT
FROM (SELECT ROWNUM D,
ROWNUM - 2 + TO_NUMBER(TO_CHAR(TRUNC(SYSDATE, 'MM'), 'D')) P,
TO_CHAR(TRUNC(SYSDATE, 'MM') - 1 + ROWNUM, 'D') DOW
FROM ALL_OBJECTS
WHERE ROWNUM <=
TO_NUMBER(TO_CHAR(LAST_DAY(TO_DATE(SYSDATE)), 'DD')))
GROUP BY TRUNC(P / 7)
ORDER BY sun NULLS FIRST;
1.2 打印年历
SELECT CASE
WHEN (NEW_YWEEK = MIN(NEW_YWEEK)
OVER(PARTITION BY MON ORDER BY NEW_YWEEK)) THEN
MON_NAME
ELSE
NULL
END AS MONTH,
NEW_YWEEK AS YWEEK,
ROW_NUMBER() OVER(PARTITION BY MON ORDER BY NEW_YWEEK) AS MWEEK,
SUM(DECODE(WDAY, '1', MDAY, NULL)) AS SUN,
SUM(DECODE(WDAY, '2', MDAY, NULL)) AS MON,
SUM(DECODE(WDAY, '3', MDAY, NULL)) AS TUE,
SUM(DECODE(WDAY, '4', MDAY, NULL)) AS WED,
SUM(DECODE(WDAY, '5', MDAY, NULL)) AS THU,
SUM(DECODE(WDAY, '6', MDAY, NULL)) AS FRI,
SUM(DECODE(WDAY, '7', MDAY, NULL)) AS SAT
FROM (SELECT DAYOFYEAR AS EVERYDAY,
TO_CHAR(DAYOFYEAR, 'mm') AS MON,
TO_CHAR(DAYOFYEAR, 'Month') AS MON_NAME,
TO_CHAR(DAYOFYEAR, 'w') AS MWEEK,
TO_CHAR(DAYOFYEAR, 'ww') AS YWEEK,
CASE
WHEN (TO_CHAR(TO_DATE(&YEAR || '0101', 'yyyymmdd'), 'd') > '1') AND
(TO_CHAR(DAYOFYEAR, 'd') <
TO_CHAR(TO_DATE(&YEAR || '0101', 'yyyymmdd'), 'd')) THEN
TO_CHAR(TO_CHAR(DAYOFYEAR, 'ww') + 1, 'fm00')
ELSE
TO_CHAR(DAYOFYEAR, 'ww')
END AS NEW_YWEEK,
TO_CHAR(DAYOFYEAR, 'd') AS WDAY,
TO_CHAR(DAYOFYEAR, 'dd') AS MDAY
FROM (SELECT TO_DATE(&YEAR || '0101', 'yyyymmdd') + LEVEL - 1 AS DAYOFYEAR
FROM DUAL
CONNECT BY LEVEL <=
TO_CHAR(TO_DATE(&YEAR || '1231', 'yyyymmdd'),
'ddd')))
GROUP BY MON, MON_NAME, NEW_YWEEK;
1.3 打印某月日历
1.3.1 方法1
ALTER session set nls_language='SIMPLIFIED CHINESE';
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'),'星期六<span "="" style="word-wrap: break-word">',to_char(everyday,'dd'))) as 星期六
from(select to_date('20170301','yyyymmdd') + level - 1 as everyDay
from dual
connect by level <= (last_day(to_date('20170301','yyyymmdd')) - to_date('20170301','yyyymmdd') +1)
)
group by to_char(everyday,'ww')
ORDER BY week;
1.3.2 方法2
select to_char(everyday,'ww') as week,
sum(decode(to_char(everyday,'d'),'1',to_char(everyday,'dd'))) as 星期日,
sum(decode(to_char(everyday,'d'),'2',to_char(everyday,'dd'))) as 星期一,
sum(decode(to_char(everyday,'d'),'3',to_char(everyday,'dd'))) as 星期二,
sum(decode(to_char(everyday,'d'),'4',to_char(everyday,'dd'))) as 星期三,
sum(decode(to_char(everyday,'d'),'5',to_char(everyday,'dd'))) as 星期四,
sum(decode(to_char(everyday,'d'),'6',to_char(everyday,'dd'))) as 星期五,
sum(decode(to_char(everyday,'d'),'7',to_char(everyday,'dd'))) as 星期六
from(select to_date('20170301','yyyymmdd') + level - 1 as everyDay
from dual
connect by level <= (last_day(to_date('20170301','yyyymmdd')) - to_date('20170301','yyyymmdd') +1)
)
group by to_char(everyday,'ww')
ORDER BY week;