oracle的周、月份、日曆
1、周
/* 關鍵在於當年第一周的第一天和最後一天怎麼算,後面只需加上7天就是一周,類推
--還有就是確定的是,一年總共只有53周
--oracle中週日為第一天 ,週六為最後一天 */
select level 周次,
(trunc(sysdate,'yyyy')-7) + (7-to_char(trunc(sysdate,'yyyy'),'d')+1) + (level-1)*7 當周第一天,
(trunc(sysdate,'yyyy')-7) + (7-to_char(trunc(sysdate,'yyyy'),'d')+1) + (level-1)*7+6 當周最後一天
from dual
connect by level<=53;
2、月份
--當年每個月的起止時間
select aa as monnum, to_date(mon,'yyyymmdd') as firstday,last_day(to_date(mon,'yyyymmdd')) as lastday
from (
select to_char(sysdate,'yyyy')||to_char(aa,'09')||'01' mon ,aa
from (
select 1 aa from dual union
select 2 aa from dual union
select 3 aa from dual union
select 4 aa from dual union
select 5 aa from dual union
select 6 aa from dual union
select 7 aa from dual union
select 8 aa from dual union
select 9 aa from dual union
select 10 aa from dual union
select 11 aa from dual union
select 12 aa from dual));
3、日曆
--from:
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
4、應用
--每週的上報情況 ,由於跨年情況存在,故在第幾周前加年份綴,如 [2010]-7 ;
select weeknumcur, sum(上報數) from (
select eventid ,上報數,'['||to_char(createtime,'yyyy')||']-'||to_number(to_char(createtime,'ww')) as weeknumcur , createtime
from statuser.tostat
where createtime between to_date('2010-12-01','yyyy-mm-dd') and to_date('2011-01-10','yyyy-mm-dd')+1 )
group by weeknumcur
order by weeknumcur;
--------------------
時間(周)上報數
[2010]-50 1
[2010]-51 16
[2010]-52 2
[2011]-1 6
實現一可調節月份日曆
思路主要是根據calendar類中的各屬性方法,來實現乙個有條件的數字迴圈,用到或有用的方法 例項乙個日曆 c calendar.getinstance 取得年份 c.get calendar.year 取得月份 c.get calendar.month 從calendar中取得的mouth是從 0...
根據輸入的日期展示當前月份的日曆
根據具體日期展示當前月份的日曆 public static void setcalendar catch parseexception e system.out.print 您輸入的日期是 dateformat.format date n 輸出日曆 system.out.print t日 t一 t二...
oracle 本月日曆
select week,min case when weekday 1 then day else null end 星期日 min case when weekday 2 then day else null end 星期一 min case when weekday 3 then day els...