`
中华国锋
  • 浏览: 41125 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 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;
分享到:
评论

相关推荐

    oracle 在报表里 实现一段时间的按正常日历展示期间的sql

    希望能帮助你。由于博主自己花了2天时间才写出来用在报表里的完全sql展示,所以要点资源分。谢谢

    Oracle9i的init.ora参数中文说明

    说明: 指定 Oracle 使用哪种日历系统作为日期格式。例如, 如果 NLS_CALENDAR 设置为 'Japanese Imperial', 那么日期格式为 'E YY-MM-DD'。即: 如果日期是 1997 年 5 月 15 日, 那么 SYSDATE 显示为 'H 09-05-15'。 ...

    在Oracle和MySQL中利用SQL查询出今年日历

    Oracle写法: select case when rank() over(partition by month order by week) = 1 then month else ' ' end month, max(一) 一, max(二) 二, max(三) 三, max(四) 四, max(五) 五, max(六) 六, max...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例202 计算程序运行时间 257 实例203 从控制台输入密码 258 9.4 Math类的使用 259 实例204 角度和弧度的转换 259 实例205 三角函数的使用 260 实例206 反三角函数的使用 261 实例207 双曲函数的使用 262 实例208 ...

    C#程序开发范例宝典(第2版).part13

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part08

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part02

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part12

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

Global site tag (gtag.js) - Google Analytics