Oracle - 记录链中的累积计数

时间:2017-08-14 13:38:24

标签: oracle plsql

我有一个带有以下列和样本数据的oracle表DM_TEMP_SUMMING_DVC_BY_FW。以下数据显示

-dmc_id ='408'有2109个设备有firmware_version ='RT1',它是第一个firmware_version,因为此firmware_image的image_prerequisite为null。

-dmc_id ='408'有40个设备将firmware_version ='RT2'从“RT1”移开,依此类推。

要求是创建firmware_versions链和移动链的设备数量。例如:

Firmware_version movement is RT1-->RT2-->RT3-->RT4
so the count of devices moved from RT1 to RT4 is (RT1+RT2+RT3+RT4)
count of devices moved from RT2 to RT4 is (RT2+RT3+RT4)
count of devices moved from RT3 to RT4 is (RT3+RT4)
count of devices at last firmware_version is RT4

以下是表DM_TEMP_SUMMING_DVC_BY_FW中的输入数据

   +-----------+-------+-----------------+-----------------+------------------+-------------------+
device_count| dmc_id| firmware_version| cg_id           |image_prerequisite| count_within_dmcid|
+-----------+-------+-----------------+-----------------+------------------+-------------------+
| 40        | 408   |RT2              |0                |RT1               | 24028             |
| 24        | 408   |RT3              |0                |RT2               | 24028             |
| 18        | 408   |RT4              |0                |RT3               | 24028             |
| 2109      | 408   |RT1              |0                |null              | 24028             |
| 1         | 1422   |RT1              |0                |null              | 7                 |
| 1         | 1422   |RT2              |0                |RT1               | 7                 |
| 1         | 408   |RT1              |HFOTA-0000000041 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000000334 |null              | 2                 |
| 1         | 408   |RT1              |HFOTA-0000000359 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000000441 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000001885 |null              | 2                 |
| 4         | 408   |SVP01            |0                |null              | 24028             |
| 11        | 408   |Sanity01         |0                |null              | 24028             |
| 1         | 408   |Sanity1          |0                |null              | 24028             |
| 6         | 408   |TB_HT01          |0                |null              | 24028             |
| 1         | 408   |TEST_1           |0                |null              | 24028             |
| 5         | 408   |TK_ST001         |0                |null              | 24028             |
+-----------+-------+-----------------+-----------------+------------------+-------------------+

查询我已执行:

SELECT
chain,
firmware_version as fw,
device_count as cnt,
dmc_id,
cg_id,
count_within_dmcid,
sum( device_count ) over ( partition by dmc_id, cg_id order by lev desc ) as cumm
FROM (
    select t.* , level lev, 
           sys_connect_by_path( firmware_version, '/' ) as chain
    from DM_TEMP_SUMMING_DVC_BY_FW t
    START WITH image_prerequisite IS null
    CONNECT BY nocycle PRIOR firmware_version=image_prerequisite
                   AND PRIOR dmc_id = dmc_id
                   AND PRIOR cg_id  = cg_id 
) x
order by dmc_id, cg_id, lev desc
;

结果查询返回:

+-----------------+---------+-----+------+----------------+------------------+----+
chain             |fw       |cnt  |dmc_id|  cg_id         |count_within_dmcid|cumm|
+-----------------+---------+-----+------+----------------+------------------+----+
|/RT1/RT2/RT3/RT4 | RT4     |   18| 408  |  0             | 24028            |  18|
|/RT1/RT2/RT3     | RT3     |   24| 408  |  0             | 24028            |  42|
|/RT1/RT2         | RT2     |   40| 408  |  0             | 24028            |  82|
|/Sanity1         |Sanity1  |   1 | 408  |  0             | 24028            |2219|
|/TB_HT01         |TB_HT01  |   6 | 408  |  0             | 24028            |2219|
|/Sanity01        |Sanity01 |   11| 408  |  0             | 24028            |2219|
|/SVP01           |SVP01    |   4 | 408  |  0             | 24028            |2219|
|/RT1             | RT1     |2109 | 408  |  0             | 24028            |2219|
|/TEST_1          |TEST_1   |   1 | 408  |  0             | 24028            |2219|
|/TK_ST001        |TK_ST001 |   5 | 408  |  0             | 24028            |2219|
|/RT1             |RT1      |   1 | 408  |HFOTA-0000000041|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000334|     2            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000359|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000441|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000001885|     2            |  1 |
|/RT1/RT2         |RT2      |1    |1422  |    0           |     7            |  1 |
|/RT1             |RT1      |1    |1422  |  0             |     7            |  2 |
+-----------------+---------+-----+------+----------------+------------------+----+

预期结果:

+-----------------+---------+-----+------+----------------+------------------+----+
chain             |fw       |cnt  |dmc_id|  cg_id         |count_within_dmcid|cumm|
+-----------------+---------+-----+------+----------------+------------------+----+
|/RT1/RT2/RT3/RT4 | RT4     |   18| 408  |  0             | 24028            |  18|
|/RT1/RT2/RT3     | RT3     |   24| 408  |  0             | 24028            |  42|
|/RT1/RT2         | RT2     |   40| 408  |  0             | 24028            |  82|
|/Sanity1         |Sanity1  |   1 | 408  |  0             | 24028            |  1 |
|/TB_HT01         |TB_HT01  |   6 | 408  |  0             | 24028            |  6 |
|/Sanity01        |Sanity01 |   11| 408  |  0             | 24028            | 11 |
|/SVP01           |SVP01    |   4 | 408  |  0             | 24028            |  4 |
|/RT1             | RT1     |2109 | 408  |  0             | 24028            |2191|
|/TEST_1          |TEST_1   |   1 | 408  |  0             | 24028            |  1 |
|/TK_ST001        |TK_ST001 |   5 | 408  |  0             | 24028            |  5 |
|/RT1             |RT1      |   1 | 408  |HFOTA-0000000041|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000334|     2            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000359|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000000441|     1            |  1 |
|/RT1             |RT1      |1    | 408  |HFOTA-0000001885|     2            |  1 |
|/RT1/RT2         |RT2      |1    |1422  |  0             |     7            |  1 |
|/RT1             |RT1      |1    |1422  |  0             |     7            |  2 |
+-----------------+---------+-----+------+----------------+------------------+----+

请为此问题提出任何解决方案。

提前致谢!

拆分链的另一种情况 -

在DM_TEMP_SUMMING_DVC_BY_FW表中输入数据。

+-----------+-------+-----------------+-----------------+------------------+-------------------+
device_count| dmc_id| firmware_version| cg_id           |image_prerequisite| count_within_dmcid|
+-----------+-------+-----------------+-----------------+------------------+-------------------+
| 5         | 3345  |SU.B             |0000000000000000 |SU.A              | 93                |
| 6         | 3345  |SU.C             |0000000000000000 |SU.B              | 93                |
| 8         | 3345  |SU.D             |0000000000000000 |SU.C              | 93                |
| 8         | 3345  |SU.E             |0000000000000000 |SU.C              | 93                |
| 20        | 3345  |SU.F             |0000000000000000 |SU.D              | 93                |
| 20        | 3345  |SU.F             |0000000000000000 |SU.E              | 93                |
| 10        | 3345  |SU.G             |0000000000000000 |SU.F              | 93                |
| 11        | 3345  |SU.H             |0000000000000000 |SU.F              | 93                |
| 20        | 3345  |SU.I             |0000000000000000 |SU.G              | 93                |
| 20        | 3345  |SU.I             |0000000000000000 |SU.H              | 93                |
| 5         | 3345  |SU.A             |0000000000000000 |null              | 93                |
| 40        | 408   |RT2              |0000000000000000 |RT1               | 24028             |
| 24        | 408   |RT3              |0000000000000000 |RT2               | 24028             |
| 18        | 408   |RT4              |0000000000000000 |RT3               | 24028             |
| 2109      | 408   |RT1              |0000000000000000 |null              | 24028             |
| 1         | 142   |RT1              |0000000000000000 |null              | 7                 |
| 1         | 142   |RT2              |0000000000000000 |RT1               | 7                 |
| 1         | 408   |RT1              |HFOTA-0000000041 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000000334 |null              | 2                 |
| 1         | 408   |RT1              |HFOTA-0000000359 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000000441 |null              | 1                 |
| 1         | 408   |RT1              |HFOTA-0000001885 |null              | 2                 |
| 4         | 408   |SVP01            |0000000000000000 |null              | 24028             |
| 11        | 408   |Sanity01         |0000000000000000 |null              | 24028             |
| 1         | 408   |Sanity1          |0000000000000000 |null              | 24028             |
| 6         | 408   |TB_HT01          |0000000000000000 |null              | 24028             |
| 1         | 408   |TEST_1           |0000000000000000 |null              | 24028             |
| 5         | 408   |TK_ST001         |0000000000000000 |null              | 24028             |
+-----------+-------+-----------------+-----------------+------------------+-------------------+

此查询在路径拆分方案数据中打破。

         SU.A (5)                    -- Level 1         
             |                                  
         SU.B (5)                    -- Level 2         
             |                                  
         SU.C (6)                    -- Level 3         
         /     \                                
(8) SU.D     SU.E (8)                -- Level 4         
         \     /                                
         SU.F (20)                   -- Level 5         
         /      \
(10) SU.G    SU.H (11)               -- Level 6
         \      /
         SU.I (20)                   -- Level 7

使用的查询:

SELECT chain,
       root_fw,
       firmware_version,
       device_count,
       dmc_id,
       charging_group_id,
       count_within_dmcid,
       SUM(device_count) over(PARTITION BY dmc_id, cg_id, root_fw ORDER BY lev DESC),
       lev
FROM   (SELECT t.*,
               LEVEL lev,
               sys_connect_by_path(firmware_version, '/') AS chain,
               connect_by_root(firmware_version) root_fw
        FROM   dm_temp_summing_dvc_by_fw t
        START  WITH image_prerequisite IS NULL
        CONNECT BY nocycle PRIOR firmware_version = image_prerequisite
            AND    PRIOR dmc_id = dmc_id
            AND    PRIOR cg_id = cg_id) x
ORDER  BY dmc_id,
          cg_id,
          lev DESC;

输出输出:

+-----------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+
chain                               |root_fw |fw      |device_count |dmc_id|  cg_id         |count_within_dmcid|cumm|lev|
+-----------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+
|/SU.A/SU.B/SU.C/SU.D/SU.F/SU.G/SU.I|SU.A    |SU.I    | 20          |3345  |0000000000000000|93                |80  |7  |
|/SU.A/SU.B/SU.C/SU.E/SU.F/SU.H/SU.I|SU.A    |SU.I    | 20          |3345  |0000000000000000|93                |80  |7  | 
|/SU.A/SU.B/SU.C/SU.E/SU.F/SU.G/SU.I|SU.A    |SU.I    | 20          |3345  |0000000000000000|93                |80  |7  | 
|/SU.A/SU.B/SU.C/SU.D/SU.F/SU.H/SU.I|SU.A    |SU.I    | 20          |3345  |0000000000000000|93                |80  |7  |
|/SU.A/SU.B/SU.C/SU.E/SU.F/SU.G     |SU.A    |SU.G    | 10          |3345  |0000000000000000|93                |122 |6  |
|/SU.A/SU.B/SU.C/SU.D/SU.F/SU.G     |SU.A    |SU.G    | 10          |3345  |0000000000000000|93                |122 |6  |
|/SU.A/SU.B/SU.C/SU.D/SU.F/SU.H     |SU.A    |SU.H    | 11          |3345  |0000000000000000|93                |122 |6  |
|/SU.A/SU.B/SU.C/SU.E/SU.F/SU.H     |SU.A    |SU.H    | 11          |3345  |0000000000000000|93                |122 |6  |
|/SU.A/SU.B/SU.C/SU.E/SU.F          |SU.A    |SU.F    | 20          |3345  |0000000000000000|93                |162 |5  |
|/SU.A/SU.B/SU.C/SU.D/SU.F          |SU.A    |SU.F    | 20          |3345  |0000000000000000|93                |162 |5  |
|/SU.A/SU.B/SU.C/SU.E               |SU.A    |SU.E    | 8           |3345  |0000000000000000|93                |178 |4  |
|/SU.A/SU.B/SU.C/SU.D               |SU.A    |SU.D    | 8           |3345  |0000000000000000|93                |178 |4  |
|/SU.A/SU.B/SU.C                    |SU.A    |SU.C    | 6           |3345  |0000000000000000|93                |184 |3  |
|/SU.A/SU.B                         |SU.A    |SU.B    | 5           |3345  |0000000000000000|93                |189 |2  |
|/SU.A                              |SU.A    |SU.A    | 5           |3345  |0000000000000000|93                |194 |1  |
|/RT1/RT2/RT3/RT4                   |RT1     |RT4     | 18          |408   |0000000000000000|24028             |18  |4  |
|/RT1/RT2/RT3                       |RT1     |RT3     | 24          |408   |0000000000000000|24028             |42  |3  |
|/RT1/RT2                           |RT1     |RT2     | 40          |408   |0000000000000000|24028             |82  |2  |
|/Sanity1                           |Sanity1 |Sanity1 | 1           |408   |0000000000000000|24028             |1   |1  |  
|/TB_HT01                           |TB_HT01 |TB_HT01 | 6           |408   |0000000000000000|24028             |6   |1  |
|/Sanity01                          |Sanity01|Sanity01| 11          |408   |0000000000000000|24028             |11  |1  |
|/SVP01                             |SVP01   |SVP01   | 4           |408   |0000000000000000|24028             |4   |1  |
|/RT1                               |RT1     |RT1     | 2109        |408   |0000000000000000|24028             |2191|1  |
|/TEST_1                            |TEST_1  |TEST_1  | 1           |408   |0000000000000000|24028             |1   |1  |
|/TK_ST001                          |TK_ST001|TK_ST001| 5           |408   |0000000000000000|24028             |5   |1  |
|/RT1                               |RT1     |RT1     | 1           |408   |HFOTA-0000000041|1                 |1   |1  |
|/RT1                               |RT1     |RT1     | 1           |408   |HFOTA-0000000334|2                 |1   |1  |
|/RT1                               |RT1     |RT1     | 1           |408   |HFOTA-0000000359|1                 |1   |1  |
|/RT1                               |RT1     |RT1     | 1           |408   |HFOTA-0000000441|1                 |1   |1  |
|/RT1                               |RT1     |RT1     | 1           |408   |HFOTA-0000001885|2                 |1   |1  |
|/RT1/RT2                           |RT1     |RT2     | 1           |1422  |0000000000000000|7                 |1   |2  |
|/RT1                               |RT1     |RT1     | 1           |1422  |0000000000000000|7                 |2   |1  |
+-----------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+

链式和累积计数对于回归链A - B-C-D来说是正确的,但如果我们有分裂链,则它会对所有生成的链进行总结。

SU.A - 93   A+B+C+D+E+F+G+H+I
SU.B - 88   B+C+D+E+F+G+H+I
SU.C - 83   C+D+E+F+G+H+I
SU.D - 0    (Cummulative count will be zero if we have two or more FW at same level)
SU.E - 0    (Cummulative count will be zero if we have two or more FW at same level)
SU.F - 61   F+G+H+I
SU.G - 0    Cummulative count will be zero if we have two or more FW at same level)
SU.H - 0    Cummulative count will be zero if we have two or more FW at same level)
SU.I - 20   I

此拆分方案中的预期输出:

+---------------------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+
chain                                         |root_fw |fw      |device_count |dmc_id|  cg_id         |count_within_dmcid|cumm|lev|
+---------------------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+
|/SU.A/SU.B/SU.C/SU.D/SU.E/SU.F/SU.G/SU.H/SU.I|SU.A    |SU.I    | 20          |3345  |0000000000000000|93                |20  |7  |
|/SU.A/SU.B/SU.C/SU.D/SU.E/SU.F/SU.G/SU.H/    |SU.A    |SU.H    | 11          |3345  |0000000000000000|93                |0   |6  |
|/SU.A/SU.B/SU.C/SU.D/SU.E/SU.F/SU.G          |SU.A    |SU.G    | 10          |3345  |0000000000000000|93                |0   |6  |
|/SU.A/SU.B/SU.C/SU.D/SU.E/SU.F               |SU.A    |SU.F    | 20          |3345  |0000000000000000|93                |61  |5  |
|/SU.A/SU.B/SU.C/SU.D/SU.E                    |SU.A    |SU.E    | 8           |3345  |0000000000000000|93                |0   |4  |
|/SU.A/SU.B/SU.C/SU.D                         |SU.A    |SU.D    | 8           |3345  |0000000000000000|93                |0   |4  |
|/SU.A/SU.B/SU.C                              |SU.A    |SU.C    | 6           |3345  |0000000000000000|93                |83  |3  |
|/SU.A/SU.B                                   |SU.A    |SU.B    | 5           |3345  |0000000000000000|93                |88  |2  |
|/SU.A                                        |SU.A    |SU.A    | 5           |3345  |0000000000000000|93                |93  |1  |
|/RT1/RT2/RT3/RT4                             |RT1     |RT4     | 18          |408   |0000000000000000|24028             |18  |4  |
|/RT1/RT2/RT3                                 |RT1     |RT3     | 24          |408   |0000000000000000|24028             |42  |3  |
|/RT1/RT2                                     |RT1     |RT2     | 40          |408   |0000000000000000|24028             |82  |2  |
|/Sanity1                                     |Sanity1 |Sanity1 | 1           |408   |0000000000000000|24028             |1   |1  |  
|/TB_HT01                                     |TB_HT01 |TB_HT01 | 6           |408   |0000000000000000|24028             |6   |1  |
|/Sanity01                                    |Sanity01|Sanity01| 11          |408   |0000000000000000|24028             |11  |1  |
|/SVP01                                       |SVP01   |SVP01   | 4           |408   |0000000000000000|24028             |4   |1  |
|/RT1                                         |RT1     |RT1     | 2109        |408   |0000000000000000|24028             |2191|1  |
|/TEST_1                                      |TEST_1  |TEST_1  | 1           |408   |0000000000000000|24028             |1   |1  |
|/TK_ST001                                    |TK_ST001|TK_ST001| 5           |408   |0000000000000000|24028             |5   |1  |
|/RT1                                         |RT1     |RT1     | 1           |408   |HFOTA-0000000041|1                 |1   |1  |
|/RT1                                         |RT1     |RT1     | 1           |408   |HFOTA-0000000334|2                 |1   |1  |
|/RT1                                         |RT1     |RT1     | 1           |408   |HFOTA-0000000359|1                 |1   |1  |
|/RT1                                         |RT1     |RT1     | 1           |408   |HFOTA-0000000441|1                 |1   |1  |
|/RT1                                         |RT1     |RT1     | 1           |408   |HFOTA-0000001885|2                 |1   |1  |
|/RT1/RT2                                     |RT1     |RT2     | 1           |1422  |0000000000000000|7                 |1   |2  |
|/RT1                                         |RT1     |RT1     | 1           |1422  |0000000000000000|7                 |2   |1  |
+---------------------------------------------+--------+--------+-------------+------+----------------+------------------+----+---+

1 个答案:

答案 0 :(得分:1)

看起来你缺少的就是对root firmware_version进行分区,你可以使用connect_by_root()进行分区,如下所示:

WITH dm_temp_summing_dvc_by_fw AS (SELECT 40 device_count, 408 dmc_id, 'RT2' firmware_version, '0000' cg_id, 'RT1' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 24 device_count, 408 dmc_id, 'RT3' firmware_version, '0000' cg_id, 'RT2' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 18 device_count, 408 dmc_id, 'RT4' firmware_version, '0000' cg_id, 'RT3' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 2109 device_count, 408 dmc_id, 'RT1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 1422 dmc_id, 'RT1' firmware_version, '0000' cg_id, NULL image_prerequisite, 7 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 1422 dmc_id, 'RT2' firmware_version, '0000' cg_id, 'RT1' image_prerequisite, 7 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA1' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA2' cg_id, NULL image_prerequisite, 2 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA3' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA4' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA5' cg_id, NULL image_prerequisite, 2 count_within_dmcid FROM dual UNION ALL
                                   SELECT 4 device_count, 408 dmc_id, 'SVP01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 11 device_count, 408 dmc_id, 'Sanity01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'Sanity1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 6 device_count, 408 dmc_id, 'TB_HT01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'TEST_1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 5 device_count, 408 dmc_id, 'TK_ST001' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual)
SELECT chain,
       root_fw,
       firmware_version AS fw,
       device_count AS cnt,
       dmc_id,
       cg_id,
       lev,
       count_within_dmcid,
       SUM(device_count) over(PARTITION BY dmc_id, cg_id, root_fw ORDER BY lev DESC) AS cumm
FROM   (SELECT t.*,
               LEVEL lev,
               sys_connect_by_path(firmware_version, '/') AS chain,
               connect_by_root(firmware_version) root_fw
        FROM   dm_temp_summing_dvc_by_fw t
        START  WITH image_prerequisite IS NULL
        CONNECT BY nocycle PRIOR firmware_version = image_prerequisite
            AND    PRIOR dmc_id = dmc_id
            AND    PRIOR cg_id = cg_id) x
ORDER  BY dmc_id,
          cg_id,
          lev DESC;

结果如下:

CHAIN             ROOT_FW  FW              CNT     DMC_ID CG_ID         LEV COUNT_WITHIN_DMCID       CUMM
----------------- -------- -------- ---------- ---------- ------ ---------- ------------------ ----------
/RT1/RT2/RT3/RT4  RT1      RT4              18        408 0000            4              24028         18
/RT1/RT2/RT3      RT1      RT3              24        408 0000            3              24028         42
/RT1/RT2          RT1      RT2              40        408 0000            2              24028         82
/Sanity1          Sanity1  Sanity1           1        408 0000            1              24028          1
/TB_HT01          TB_HT01  TB_HT01           6        408 0000            1              24028          6
/Sanity01         Sanity01 Sanity01         11        408 0000            1              24028         11
/SVP01            SVP01    SVP01             4        408 0000            1              24028          4
/RT1              RT1      RT1            2109        408 0000            1              24028       2191
/TEST_1           TEST_1   TEST_1            1        408 0000            1              24028          1
/TK_ST001         TK_ST001 TK_ST001          5        408 0000            1              24028          5
/RT1              RT1      RT1               1        408 HFOTA1          1                  1          1
/RT1              RT1      RT1               1        408 HFOTA2          1                  2          1
/RT1              RT1      RT1               1        408 HFOTA3          1                  1          1
/RT1              RT1      RT1               1        408 HFOTA4          1                  1          1
/RT1              RT1      RT1               1        408 HFOTA5          1                  2          1
/RT1/RT2          RT1      RT2               1       1422 0000            2                  7          1
/RT1              RT1      RT1               1       1422 0000            1                  7          2

ETA:

由于额外要求处理firmeware彼此处于同一水平的情况,我认为*这样做符合您的要求:

WITH dm_temp_summing_dvc_by_fw AS (SELECT 40 device_count, 408 dmc_id, 'RT2' firmware_version, '0000' cg_id, 'RT1' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 24 device_count, 408 dmc_id, 'RT3' firmware_version, '0000' cg_id, 'RT2' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 18 device_count, 408 dmc_id, 'RT4' firmware_version, '0000' cg_id, 'RT3' image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 2109 device_count, 408 dmc_id, 'RT1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 1422 dmc_id, 'RT1' firmware_version, '0000' cg_id, NULL image_prerequisite, 7 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 1422 dmc_id, 'RT2' firmware_version, '0000' cg_id, 'RT1' image_prerequisite, 7 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA1' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA2' cg_id, NULL image_prerequisite, 2 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA3' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA4' cg_id, NULL image_prerequisite, 1 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'RT1' firmware_version, 'HFOTA5' cg_id, NULL image_prerequisite, 2 count_within_dmcid FROM dual UNION ALL
                                   SELECT 4 device_count, 408 dmc_id, 'SVP01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 11 device_count, 408 dmc_id, 'Sanity01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'Sanity1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 6 device_count, 408 dmc_id, 'TB_HT01' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 1 device_count, 408 dmc_id, 'TEST_1' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 5 device_count, 408 dmc_id, 'TK_ST001' firmware_version, '0000' cg_id, NULL image_prerequisite, 24028 count_within_dmcid FROM dual UNION ALL
                                   SELECT 5 device_count, 3345 dmc_id, 'SU.B' firmware_version, '0000' cg_id, 'SU.A' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 6 device_count, 3345 dmc_id, 'SU.C' firmware_version, '0000' cg_id, 'SU.B' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 8 device_count, 3345 dmc_id, 'SU.D' firmware_version, '0000' cg_id, 'SU.C' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 8 device_count, 3345 dmc_id, 'SU.E' firmware_version, '0000' cg_id, 'SU.C' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 20 device_count, 3345 dmc_id, 'SU.F' firmware_version, '0000' cg_id, 'SU.D' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 20 device_count, 3345 dmc_id, 'SU.F' firmware_version, '0000' cg_id, 'SU.E' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 10 device_count, 3345 dmc_id, 'SU.G' firmware_version, '0000' cg_id, 'SU.F' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 11 device_count, 3345 dmc_id, 'SU.H' firmware_version, '0000' cg_id, 'SU.F' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 20 device_count, 3345 dmc_id, 'SU.I' firmware_version, '0000' cg_id, 'SU.G' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 20 device_count, 3345 dmc_id, 'SU.I' firmware_version, '0000' cg_id, 'SU.H' image_prerequisite, 93 count_within_dmcid FROM dual UNION ALL
                                   SELECT 5 device_count, 3345 dmc_id, 'SU.A' firmware_version, '0000' cg_id, NULL image_prerequisite, 93 count_within_dmcid FROM dual),
                            t1 AS (SELECT device_count,
                                          dmc_id,
                                          CASE WHEN COUNT(image_prerequisite) OVER (PARTITION BY dmc_id, cg_id, image_prerequisite) > 1
                                                    THEN listagg(firmware_version, '~') WITHIN GROUP (ORDER BY firmware_version) OVER (PARTITION BY dmc_id, cg_id, image_prerequisite)
                                               ELSE firmware_version
                                          END firmware_version,
                                          cg_id,
                                          image_prerequisite,
                                          count_within_dmcid
                                   FROM   dm_temp_summing_dvc_by_fw),
                            t2 AS (SELECT sum(device_count) device_count,
                                          dmc_id,
                                          firmware_version,
                                          cg_id,
                                          image_prerequisite,
                                          count_within_dmcid
                                   FROM   t1
                                   GROUP BY dmc_id,
                                            firmware_version,
                                            cg_id,
                                            image_prerequisite,
                                            count_within_dmcid),
                            t3 AS (SELECT t.*,
                                          LEVEL lev,
                                          sys_connect_by_path(firmware_version, '/') AS chain,
                                          connect_by_root(firmware_version) root_fw,
                                          row_number() OVER (PARTITION BY dmc_id, cg_id, firmware_version, connect_by_root(firmware_version) ORDER BY LEVEL DESC, sys_connect_by_path(firmware_version, '/')) rn
                                   FROM   t2 t
                                   START  WITH image_prerequisite IS NULL
                                   CONNECT BY nocycle PRIOR regexp_substr(firmware_version, '[^~]*') = image_prerequisite
                                       AND    PRIOR dmc_id = dmc_id
                                       AND    PRIOR cg_id = cg_id)
SELECT chain,
       root_fw,
       firmware_version AS fw,
       device_count AS cnt,
       dmc_id,
       cg_id,
       lev,
       count_within_dmcid,
       rn,
       SUM(device_count) over(PARTITION BY dmc_id, cg_id, root_fw ORDER BY lev DESC) AS cumm
FROM   t3
ORDER  BY dmc_id,
          cg_id,
          lev DESC;

给了我们:

CHAIN                                         ROOT_FW    FW                     CNT     DMC_ID CG_ID         LEV COUNT_WITHIN_DMCID         RN       CUMM
--------------------------------------------- ---------- --------------- ---------- ---------- ------ ---------- ------------------ ---------- ----------
/RT1/RT2/RT3/RT4                              RT1        RT4                     18        408 0000            4              24028          1         18
/RT1/RT2/RT3                                  RT1        RT3                     24        408 0000            3              24028          1         42
/RT1/RT2                                      RT1        RT2                     40        408 0000            2              24028          1         82
/SVP01                                        SVP01      SVP01                    4        408 0000            1              24028          1          4
/RT1                                          RT1        RT1                   2109        408 0000            1              24028          1       2191
/Sanity01                                     Sanity01   Sanity01                11        408 0000            1              24028          1         11
/Sanity1                                      Sanity1    Sanity1                  1        408 0000            1              24028          1          1
/TB_HT01                                      TB_HT01    TB_HT01                  6        408 0000            1              24028          1          6
/TEST_1                                       TEST_1     TEST_1                   1        408 0000            1              24028          1          1
/TK_ST001                                     TK_ST001   TK_ST001                 5        408 0000            1              24028          1          5
/RT1                                          RT1        RT1                      1        408 HFOTA1          1                  1          1          1
/RT1                                          RT1        RT1                      1        408 HFOTA2          1                  2          1          1
/RT1                                          RT1        RT1                      1        408 HFOTA3          1                  1          1          1
/RT1                                          RT1        RT1                      1        408 HFOTA4          1                  1          1          1
/RT1                                          RT1        RT1                      1        408 HFOTA5          1                  2          1          1
/RT1/RT2                                      RT1        RT2                      1       1422 0000            2                  7          1          1
/RT1                                          RT1        RT1                      1       1422 0000            1                  7          1          2
/SU.A/SU.B/SU.C/SU.D~SU.E/SU.F/SU.G~SU.H/SU.I SU.A       SU.I                    20       3345 0000            7                 93          1         20
/SU.A/SU.B/SU.C/SU.D~SU.E/SU.F/SU.G~SU.H      SU.A       SU.G~SU.H               21       3345 0000            6                 93          1         41
/SU.A/SU.B/SU.C/SU.D~SU.E/SU.F                SU.A       SU.F                    20       3345 0000            5                 93          1         61
/SU.A/SU.B/SU.C/SU.D~SU.E                     SU.A       SU.D~SU.E               16       3345 0000            4                 93          1         77
/SU.A/SU.B/SU.C                               SU.A       SU.C                     6       3345 0000            3                 93          1         83
/SU.A/SU.B                                    SU.A       SU.B                     5       3345 0000            2                 93          1         88
/SU.A                                         SU.A       SU.A                     5       3345 0000            1                 93          1         93

这个最新查询的作用是首先找出是否存在彼此处于同一级别的固件,如果是,我们将它们列在一起(这里,我使用~作为结果清晰的分隔符 - 没有什么可以阻止你使用与在sys_connect_by_path中使用相同的分隔符。

一旦我们有了,我们可以进行连接,但是我们只需要查看新计算的固件列中的第一个条目(否则我们不会在image_prerequisite列中找到匹配项)。然后它就像上一次查询一样计算结果。