2

我有一个看起来像(Oracle 11.2.0.2.0)的表:

ParentID    ID       AllowsEntry
NULL        A188     N
A188        1881     Y
NULL        A189     N
A189        1891     Y
1891        189A     Y

业务规则允许非叶级元素具有数据输入,但我需要像不需要那样报告它们我需要能够查询数据库以产生如下输出:

ParentID    ID
NULL        A188
A188        1881
NULL        A189
A189        1891_
1891_       189A

所以基本上我需要将允许数据输入到叶级别的中间分支下推。新叶子需要汇总到一个重命名的分支:

Old Tree       New Tree
A188           A188         -- remains the same, no data entry at this level
  1881           1881       -- remains the same, data entry allowed at leaf
A189           A189         -- remains the same, no data entry at this level
  1891           1891_      -- this is the level that is wrong
    189A           1891     -- 1891 is push down into a new 1891_ level
                   189A     -- and rolls up into the new level.
                   189B     -- etc.

谢谢您的帮助

4

1 回答 1

0

我认为您想向具有AllowsEnrtry = 'Y'. 节点元素也应该使用尾随下划线重命名。

您可以使用 UNION ALL 模拟修改后的数据并在此视图上运行递归查询:

SQL> WITH DATA AS (
  2     SELECT NULL parentid, 'A188' ID, 'N' allowsEntry FROM dual
  3     UNION ALL SELECT 'A188', '1881', 'Y' FROM dual
  4     UNION ALL SELECT  NULL , 'A189', 'N' FROM dual
  5     UNION ALL SELECT 'A189', '1891', 'Y' FROM dual
  6     UNION ALL SELECT '1891', '189A', 'Y' FROM dual
  7     UNION ALL SELECT '1891', '189B', 'Y' FROM dual
  8  ), leaf_also_nodes AS (
  9     SELECT *
 10       FROM DATA
 11      WHERE allowsEntry = 'Y'
 12        AND ID IN (SELECT parentid FROM DATA)
 13  ), data_plus AS (
 14     SELECT d.parentid,
 15            d.id,
 16            CASE WHEN l.id IS NOT NULL THEN l.id || '_' ELSE d.id
 17            END display_name
 18       FROM DATA d
 19       LEFT JOIN leaf_also_nodes l ON d.id = l.id
 20     UNION ALL
 21     SELECT ID, NULL, ID FROM leaf_also_nodes
 22  )
 23  SELECT rpad(' ', (LEVEL - 1) * 2, ' ') || display_name tree
 24    FROM data_plus
 25   START WITH parentid IS NULL
 26  CONNECT BY (PRIOR ID = parentid);

TREE
-----------------------------------------
A188
  1881
A189
  1891_
    189A
    189B
    1891
于 2013-02-27T17:31:10.080 回答