我的DW中有一个TIME维度,我想添加一个阵营来填补今年的赛季。 我想做这样的事情:
create or replace
PROCEDURE "PROC_UPDATE_SEASON"
IS
CURSOR curs IS
SELECT * FROM indw.time FOR UPDATE;
cur_d NUMBER;
cur_m NUMBER;
start_d NUMBER;
start_m NUMBER;
end_d NUMBER;
end_m NUMBER;
cur_Date DATE;
BEGIN
FOR cs IN curs
LOOP
cur_d := to_number(to_char(cs.time_DAY,'dd'));
cur_m := to_number(to_char(cs.time_DAY,'mm'));
cur_date:= cs.time_DAY; -- this is my date dd-mm-yyyy or dd/mm/yyyy
if ((cur_date in format dd/mm is >= 21/12 and <=31/12) OR cur_date>=01/01 and <=20/03)
UPDATE time t set t.time_SEASON = 'Winter' WHERE CURRENT OF curs;
else if cur_date_in format dd/mm is between a date and another then it's Spring)
-- and so on ...
END LOOP;
END;
有没有办法用to_char和to_date来实现这个目的?我正在尝试,但总是得到错误:(分别比较月份和日期将是一个巨大的,IMO,愚蠢的工作。
你能给我一些提示吗?
我真的只使用像dd / mm&lt; = another_date_with_only_day_and_month
这样的东西答案 0 :(得分:5)
无需游标和循环:
update indw.time
set t.time_SEASON = case
when to_char(time_DAY,'MMDD') between '1221' and '1231'
or to_char(time_DAY,'MMDD') between '0101' and '0320'
then 'Winter'
when ...
end
更好的是,在Oracle 11G中,您可以根据该CASE表达式向表中添加一个虚拟列,以便每行自动拥有正确的time_season而无需更新。
答案 1 :(得分:0)
我无法访问Oracle以便为您播放选项。但我确实有一条建议:
- 如果此代码经常运行,请谨慎使用字符串转换和字符串比较,与数字处理相比,它们通常非常慢
- 但是,如果只是为了偶尔使用,请选择最简单的方法,并使用螺杆性能:)
考虑到这一点,我会使用EXTRACT ...... (注意,正如我所说,我无法测试这个)
UPDATE indw.time
SET t.time_SEASON = CASE
WHEN EXTRACT(MONTH, time_DAY) < 3 THEN 'Winter'
WHEN EXTRACT(MONTH, time_DAY) = 3 THEN
CASE WHEN EXTRACT(DAY, time_DAY) <= 20 THEN 'Winter' ELSE 'Spring' END
WHEN EXTRACT(MONTH, time_DAY) < 6 THEN 'Spring'
WHEN EXTRACT(MONTH, time_DAY) = 6 THEN
CASE WHEN EXTRACT(DAY, time_DAY) <= 20 THEN 'Spring' ELSE 'Summer' END
WHEN EXTRACT(MONTH, time_DAY) < 9 THEN 'Summer'
WHEN EXTRACT(MONTH, time_DAY) = 9 THEN
CASE WHEN EXTRACT(DAY, time_DAY) <= 20 THEN 'Summer' ELSE 'Autumn' END
WHEN EXTRACT(MONTH, time_DAY) < 12 THEN 'Autumn'
WHEN EXTRACT(MONTH, time_DAY) = 12 THEN
CASE WHEN EXTRACT(DAY, time_DAY) <= 20 THEN 'Autumn' ELSE 'Winter' END
END
答案 2 :(得分:0)
我会这样从日期获取季节:
CASE
WHEN EXTRACT(MONTH FROM time_DAY) IN (12,1,2) THEN 'winter'
WHEN EXTRACT(MONTH FROM time_DAY) IN (3,4,5) THEN 'spring'
WHEN EXTRACT(MONTH FROM time_DAY) IN (6,7,8) THEN 'summer'
WHEN EXTRACT(MONTH FROM time_DAY) IN (9,10,11) THEN 'fall'
END season