Oracle10g 如何从oracle项目中记录可用的列中查找parentid

Oracle10g 如何从oracle项目中记录可用的列中查找parentid,oracle10g,Oracle10g,我有四个专栏 Item Subitem subitemid Itemid A1001 B110 1111 2111 A1002 B112 1112 2112 A1003 B113 1113 2113 B1001 C113 1114 2114 B1002 C114 1116

我有四个专栏

Item Subitem subitemid Itemid A1001 B110 1111 2111 A1002 B112 1112 2112 A1003 B113 1113 2113 B1001 C113 1114 2114 B1002 C114 1116 2115 C1001 D114 1117 2116 C1002 D115 1118 2117 D1001 E115 1119 2118 E1002 F116 1120 2119 E1002 G117 1121 2120 F1003 H118 1122 2121 G1004 I119 1123 2122 H1001 J120 1124 2123 项目子项目子项目ID项目ID A1001 B110 1111 2111 A1002 B112 1112 2112 A1003 B113 1113 2113 B1001 C113 1114 2114 B1002 C114 1116 2115 C1001 D114 1117 2116 C1002 D115 1118 2117 D1001 E115 1119 2118 E1002 F116 1120 2119 E1002 G117 1121 2120 F1003 H118 1122 2121 G1004 I119 1123 2122 H1001 J120 1124 2123 子项在项中可用,子项ID是子项的主键。 在项目栏中,项目所属的每一级别在项目内部有2个空格缩进。 需要一个select查询,其中可以为parentsubitemid显示项中可用项的subitemid的表显示更多列


例如,A1003是C113的父项,C114的子项id 1113将是C113的父项id,C114和B1002是D114和D115的父项,其中1116是B1002的父项子项id。

我完全听不懂你的最后一段。我不知道这是计算出一行的值,还是多行的多个值。您可以尝试将其细分,并显示所有示例数据的预期结果,以及您当前的查询吗?B113是C113和C114的parentsubitem,就像这样C114是D114和D115的parentsubitem,因此新列应该显示C113和C114的parentsubitemid 1113,D114和D115的parentsubitemid 1116。
with your_table as (
select 'A1001' Item,        'B110' Subitem,      1111 subitemid,       2111 Itemid from dual
union all select  'A1002',        'B112',       1112,        2112 from dual
union all select  'A1003',        'B113',       1113,        2113 from dual    
union all select  '  B1001',      'C113',       1114,        2114 from dual
union all select  '  B1002',      'C114',       1116,        2115 from dual
union all select  '    C1001',   'D114',       1117,        2116 from dual
union all select  '    C1002',   'D115',       1118,        2117 from dual
union all select  'D1001',        'E115',       1119,        2118 from dual
union all select  '  E1002',      'F116',       1120,        2119 from dual
union all select  '  E1002',      'G117',       1121,        2120 from dual
union all select  '    F1003',   'H118',       1122,        2121 from dual
union all select  '    G1004',   'I119',       1123,        2122 from dual
union all select  'H1001',        'J120',       1124,        2123 from dual
), 
tab_level as (
  -- calculate level for each row
  select item, subitem, subitemid, itemid, 
         -- regexp_instr finds a position of the first not space symbol 
         (regexp_instr(item, '[^ ]')-1)/2+1 lvl 
  from your_table
),
tree as (
  -- make a hierarchical structure (find parent for each row) 
  select item, subitem, subitemid, itemid, 
         lag(itemid, lvl-1) over(order by itemid) parent_itemid
         -- analytic function which gives (lvl-1)th previous itemid
         -- it is important that the rows sorted by itemid go in the same order as in your example otherwise this query won't work 
  from tab_level
) 
-- final query, use CONNECT BY to find the desired extra info
select item, subitem, subitemid, itemid,    
       -- unary operator, gives the "root" itemid     
       connect_by_root itemid parentsubitemid,
       -- shows all the parents separated by /
       sys_connect_by_path(item, '/') whole_path
from tree
start with itemid = parent_itemid
connect by nocycle prior itemid = parent_itemid;