该存储过程 PRO_MON_MDCODE 实现多个分类不同计算规则得到对应的分类、月份和款号,其中线下分类的款最早出现时间会在20230101,最晚是当前月份后12月,电商的款取商品维表的23,24,25年商品年份的A款,其他业务分类逻辑('A-线上','B电商公司','C品牌公司')的款最早出现时间会在20230101,晚是当前月份后12月,规则是本年从1月至第1个有值的月份,大于等于1月,小于等于第1个有值月;从第1个有值至第2个有值,大于第1个有值,小于等于第二个有值范围内月;从第2个有值至第3个有值,大于第2个有值,小于等于第3个有值范围内月;......至最后一个有值月,至本年12月,大于最后一个有值月,到12月范围内月。
CREATE OR REPLACE PROCEDURE PRO_MON_MDCODE IS
BEGIN
-- 清空目标表
EXECUTE IMMEDIATE 'TRUNCATE TABLE DIM_标签';
insert into DIM_标签(分类,月份,款号)
with dt as (
select substr(t.date,1,4) 年,substr(t.date,1,6) 月 from qpms.dim_时间维表 t
where t.date >= 20230101 and t.year <= ADD_MONTHS(sysdate - 1,12)
group by substr(t.date,1,4),substr(t.date,1,6) )
select '电商' as 分类, dt.月,t.款号 from dt,
(select w.款号, w."23商品年份" from 商品维表 w where w."23商品年份" like '%A款%') t
where dt.年 = '2023'
union all
select '电商' as 分类, dt.月,t.款号 from dt,
(select w.款号, w."24商品年份" from 商品维表 w where w."24商品年份" like '%A款%') t
where dt.年 = '2024'
union all
select '电商' as 分类, dt.月,t.款号 from dt,
(select w.款号, w."25商品年份" from 商品维表 w where w."25商品年份" like '%A款%') t
where dt.年 = '2025' ;
commit;
-- 循环处理不同分类
FOR v_分类 IN (SELECT DISTINCT t.business FROM 商品基础表 t GROUP BY t.business) LOOP
-- 线下
IF v_分类.business = '线下' THEN
INSERT INTO DIM_标签(分类,月份,款号)
WITH dt AS (
SELECT substr(t.date, 1, 4) 年, substr(t.date, 1, 6) 月
FROM dim_时间维表 t
WHERE t.date >= 20230101
AND t.year <= ADD_MONTHS(SYSDATE - 1, 12)
GROUP BY substr(t.n_date, 1, 4), substr(t.n_date, 1, 6)
)
SELECT '线下' as 分类, dt.月, t.masscode as 款号
FROM dt
JOIN (SELECT * FROM 商品基础表 WHERE business = '线下') t
ON dt.月 = t.mon;
commit;
END IF;
-- 其他业务分类逻辑
----按年分组,取每个月份之前的所有月使用这个月份的款号
IF v_分类.business in ('A-线上','B电商公司','C品牌公司') then
for v_年 in (select substr(t.mon,1,4) v_year from 商品基础表 t where t.business = v_分类.business group by substr(t.mon,1,4) order by 1) loop
for v_月 in (select v_mon,substr(v_mon,5,2) v_mon_right,rownum row,count(1) over() allrw from (select t.mon v_mon from 商品基础表 t where t.business = v_分类.business and substr(t.mon,1,4) = v_年.v_year group by mon order by 1 ) t) loop
insert into DIM_标签 (分类,月份,款号)
with dt as (
select substr(t.date,1,4) 年,substr(t.date,1,6) 月 from dim_时间维表 t
where t.date >= 20230101 and t.year <= ADD_MONTHS(sysdate - 1,12)
group by substr(t.date,1,4),substr(t.date,1,6) )
select v_分类.business 分类, dt.月,t.masscode 款号 from dt,
(select * from 商品基础表 t where t.business = v_分类.business and t.mon = v_月.v_mon) t
where dt.年 = v_年.v_year and dt.月 <= v_月.v_mon
and dt.月 > nvl((select max(m.月份) from DIM_标签表 m where m.分类= v_分类.business and substr(m.月份,1,4) = v_年.v_year),0) ;
commit;
--补充不是12月是最后月份的数据
if v_月.rw = v_月.allrw and v_月.v_mon_right <> '12' then
insert into DIM_标签 (分类,月份,款号)
with dt as (
select substr(t.date,1,4) 年,substr(t.date,1,6) 月 from dim_时间维表 t
where t.date >= 20230101 and t.year <= ADD_MONTHS(sysdate - 1,12)
group by substr(t.date,1,4),substr(t.date,1,6) )
select v_主体.business 分类, dt.月 ,t.masscode 款号 from dt ,
(select * from 商品基础表 t where t.business = v_分类.business and t.mon = v_月.v_mon) t
where dt.年 = v_年.v_year and dt.月 <= substr(v_月.v_mon,1,4)||'12'
and dt.月 > nvl((select max(m.月份) from DIM_标签表 m where m.分类= v_分类.business and substr(m.月份,1,4) = v_年.v_year),0);
commit;
end if;
end loop;
end loop;
end if;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
-- 错误处理
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END PRO_MON_MDCODE ;
- 添加
EXCEPTION
块来处理可能的异常,例如在COMMIT
失败时进行回滚。
该存储过程计算不同分类下每月存在的款号时间分类,
旨在处理特定业务逻辑,该逻辑涉及将数据插入到DIM_标签
表中。这个过程包含多个部分,包括数据截断、条件判断、数据插入和事务提交。