Oracle - 多区间按权重取值逻辑 ,分时区-多层级-取配置方案https://blog.csdn.net/shijianduan1/article/details/133386281
某业务配置表,按配置的时间区间及组织层级取方案,形成报表展示出所有部门方案的取值;
例如,总公司配置20230101-20231231为方案3, 分公司配置 20230301-20230731 为方案2,部门配置20230601-20231031 为方案1,配置优先级为 部门> 分公司>总公司 ,即啥都没配则使用总公司默认值;
继上一篇拆分多区间后, 再次进行优化,目前有阶段性成果,先记录一下,等后续再有改进再发。
上一篇中 对日期区间的边界日期做了单独的拆分,没有将边界日期和其临界的 日期合并。
本篇中会对其进行优化, 单目前基础脚本只支持2个优先级,如多个优先级则需要 增加多次查询
数据源 L:优先级,2>3 , B1 开始日期, E1 结束日期
注意: 这里默认数据源的 日期区间不会重复
拆分后结果
以下是代码
WITH S AS(SELECT 3 AS L, '20230101' AS B1, '20230821' AS E1FROM DUALUNION ALLSELECT 3 AS L, '20230822' AS B1, '20231231' AS E1FROM DUALUNION ALLSELECT 2 AS L, '20230301' AS B1, '20230731' AS E1FROM DUALUNION ALLSELECT 2 AS L, '20230901' AS B1, '20230931' AS E1FROM DUAL UNION ALLSELECT 2 AS L, '20230401' AS B1, '20230631' AS E1FROM DUAL)
,A AS(SELECT S.*, ROW_NUMBER() OVER(PARTITION BY L ORDER BY B1) AS N FROM S) --对优先级重新排序,确保优先级差1
,A2 AS(SELECT A.*, NVL(PRE.E1, 0) B_PRE, NVL(NEX.B1, 20991231) AS E_NEXFROM ALEFT JOIN A PRE ON A.N = PRE.N + 1 AND PRE.L = A.LLEFT JOIN A NEX ON A.N = NEX.N - 1 AND NEX.L = A.LWHERE A.L = 2 ) , --同级别的优先级,也是有边界的
A3 AS(SELECT * FROM A WHERE L = 3)
SELECT A3.L, GREATEST(A3.B1, A2.B_PRE)as B2, A2.B1 as E2 FROM A2, A3 WHERE A3.B1 < A2.B1 AND A3.E1 > A2.B1
UNION ALL
SELECT A3.L, A2.E1 as B2, LEAST(A3.E1, A2.E_NEX) AS E2 FROM A2, A3 WHERE A3.B1 < A2.E1 AND A3.E1 > A2.E1
UNION ALL select L, B1, E1 from A2
order by L, B2;