图解使用ORACLE存储过程创建时间维表.doc
文本预览下载声明
在使用ORACLE的过程中,可能需要判断某一天是某个月的第几周,某一天是某一年的第几周。本人使用自定义函数,调用ORACLE存储过程创建时间维表,解决了该问题。
首先,在数据库内创建维度表的表结构。建表语句如下:
CREATE TABLE TIME_DIM
(
DAY_KEY CHAR(8) NOT NULL,
DAY_DATE DATE,
DAY_YEAR CHAR(4),
DAY_QUARTER CHAR(1),
DAY_MONTH CHAR(2),
WEEK_INMONTH CHAR(1),
WEEK_INYEAR CHAR(2),
DAY_INMONTH CHAR(2)
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
comment on table TIME_DIM is 时间维表;
comment on column TIME_DIM.DAY_KEY is 时间;
comment on column TIME_DIM.DAY_DATE is 日期;
comment on column TIME_DIM.DAY_YEAR is 年份;
comment on column TIME_DIM.DAY_QUARTER is 季度;
comment on column TIME_DIM.DAY_MONTH is 月份;
comment on column TIME_DIM.WEEK_INMONTH is 月份的第几周;
comment on column TIME_DIM.WEEK_INYEAR is 年份的第几周;
comment on column TIME_DIM.DAY_INMONTH is 日;
接着,定义函数get_week_month,用于计算某一日期在当月的第几周,定义如下:
create or replace function get_week_month(date1 in CHAR) return integer is
result integer;
startday char(8);
xingqi char(9); /* 本月第一天是星期几 */
week_num number; /* 本月的第一周一共有几天 */
day_num number; /* date1是这个月的第几天 */
day_num2 number;
begin
if substr(date1,7,2)=01 then
result:=1;
else
startday:=substr(date1,1,6)||01;
select to_char(to_date(startday,yyyymmdd),day) into xingqi from dual;
select (case xingqi
when 星期一 then 7
when 星期二 then 6
when 星期三 then 5
when 星期四 then 4
when 星期五 then 3
when 星期六 then 2
when 星期日 then 1
end
) into week_num
from dual;
显示全部