自学内容网 自学内容网

Oracle - 多区间按权重取值逻辑 ,分时区-多层级-取配置方案(二)


Oracle - 多区间按权重取值逻辑 ,分时区-多层级-取配置方案icon-default.png?t=O83Ahttps://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 E1
    FROM DUAL
  UNION ALL
  SELECT 3 AS L, '20230822' AS B1, '20231231' AS E1
    FROM DUAL
  UNION ALL
  SELECT 2 AS L, '20230301' AS B1, '20230731' AS E1
    FROM DUAL
  UNION ALL
  SELECT 2 AS L, '20230901' AS B1, '20230931' AS E1
    FROM DUAL 
  UNION ALL
  SELECT 2 AS L, '20230401' AS B1, '20230631' AS E1
    FROM 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_NEX
    FROM A
    LEFT JOIN A PRE ON A.N = PRE.N + 1 AND PRE.L = A.L
    LEFT JOIN A NEX ON A.N = NEX.N - 1 AND NEX.L = A.L
   WHERE 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;


原文地址:https://blog.csdn.net/shijianduan1/article/details/143903822

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!