oralce sql 建立指定時間段內的日曆資訊

2021-06-25 19:37:52 字數 2964 閱讀 7669

-- create table

create table temp_calendar

(month varchar2(6),

w_7   varchar2(2),

w_1   varchar2(2),

w_2   varchar2(2),

w_3   varchar2(2),

w_4   varchar2(2),

w_5   varchar2(2),

w_6   varchar2(2),

week  varchar2(20)

) ;-- add comments to the table 

comment on table temp_calendar

is '日期源資料表';

-- add comments to the columns 

comment on column temp_calendar.month

is '月份(格式如:200801)';

comment on column temp_calendar.w_7

is '週日的日期(如:06)';

comment on column temp_calendar.w_1

is '周一(如:07)';

comment on column temp_calendar.w_2

is '周二(如:08)';

comment on column temp_calendar.w_3

is '週三(如:09)';

comment on column temp_calendar.w_4

is '周四(如:10)';

comment on column temp_calendar.w_5

is '周五(如:11)';

comment on column temp_calendar.w_6

is '週六(如:12)';

comment on column temp_calendar.week

is '本月第幾周';

----生成 日曆資訊 

-- truncate table calendar; 

declare 

vi_begin_year number(6); 

vi_end_year number(6); 

vi_year number(6); 

vi_month number(6); 

vs_month varchar2(6); 

vs_first_day varchar2(8); 

begin 

vi_begin_year :=2000; ---start year

vi_end_year :=2100;   --- end year

vi_month :=1; 

vi_year := vi_begin_year; 

loop exit when(vi_year > vi_end_year);

for i in 1..12 loop 

if length(i) =1 then 

vs_month := to_char(vi_year)||'0'||to_char(i); 

else

vs_month := to_char(vi_year)||to_char(i); 

end if;

vs_first_day := vs_month||'01'; 

insert into temp_calendar ( month, week,w_7, w_1, w_2, w_3, w_4, w_5, w_6 ) 

select substr(vs_first_day,1,6) month, 

ceil((to_char(everyday,'dd')+(to_char(to_date(vs_first_day,'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(vs_first_day,'yyyymmdd') + level - 1 as everyday 

from dual 

connect by level <= (last_day(to_date(vs_first_day,'yyyymmdd')) 

- to_date(vs_first_day,'yyyymmdd') +1) ) 

group by ceil((to_char(everyday,'dd')+(to_char(to_date(vs_first_day,'yyyymmdd'),'d')-1))/7) ; 

commit; 

end loop; 

vi_year := vi_year+1; 

end loop; 

end;

order by a.year,a.month, a.week;

oralce sql 建立指定時間段內的日曆資訊

create table create table temp calendar month varchar2 6 w 7 varchar2 2 w 1 varchar2 2 w 2 varchar2 2 w 3 varchar2 2 w 4 varchar2 2 w 5 varchar2 2 w 6...

mysql 指定時間 MySQL查詢指定時間的資料

mysql查詢指定時間的資料 user event 使用者事件表 create time 表中儲存時間的字段 獲取當月資料 select from user event where date format create time,y m date format now y m 獲取3月份資料 sel...

crontab指定時間

使用crontab你可以在指定的時間執行乙個shell指令碼或者一系列linux命令。例如系統管理員安排乙個備份任務使其每天都執行 如何往 cron 中新增乙個作業?crontab e 0 5 root bin backup.sh 這將會在每天早上5點執行 root bin backup.sh cr...