如何在层次树形结构中设置父子关系

huangapple go评论58阅读模式
英文:

How to set parent-child relation in a hierarchy tree form

问题

我有一个 ORACLE 表格,其中的数据表示父子关系,但在我的表格中没有这样的关系。那么如何在我的表格中设置父子关系,而原本不存在呢?

我的表格中有一些示例数据,如下所示,以便理解:

ID CODE NAME PARENT_ID
5 0005 ROOT
15 0015 ASSETS
25 0025 CASH AND BANK BALANCE
35 0035 CASH IN HAND
45 0045 CASH IN VAULT
55 0055 CASH IN HAND LCY (VAULT)
60 0060 CASH IN HAND FCY (VAULT).
75 0075 CASH AT ATM
85 0085 CASH AT ATM
120 0120 BAL WITH BANGLADESH BANK
130 0130 BAL WITH BANGLADESH BANK LCY
135 0135 BAL. WITH BANGLADESH FCY

从上表中,ROOT 代表我的表格数据的根。在其下,ASSETS 代表 ROOT 的子项。在 ASSETS 下,CASH AND BANK BALANCE 是子项。在 CASH AND BANK BALANCE 下,CASH IN HANDBAL WITH BANGLADESH BANK 是子项。CASH IN VAULTCASH AT ATM (0075)CASH IN HAND 的子项。CASH IN HAND LCY (VAULT)CASH IN HAND FCY (VAULT) 分别是 CASH IN VAULT 的子项。类似地,CASH AT ATMBAL WITH BANGLADESH BANK 的子项分别是 CASH AT ATM (0085)BAL WITH BANGLADESH BANK LCYBAL. WITH BANGLADESH FCY

那么,从这些数据中如何创建子父关系的数据呢?

我的最终数据表将如下所示:

ID CODE NAME PARENT_ID
5 0005 ROOT
15 0015 ASSETS 5
25 0025 CASH AND BANK BALANCE 15
35 0035 CASH IN HAND 25
45 0045 CASH IN VAULT 35
55 0055 CASH IN HAND LCY (VAULT) 45
60 0060 CASH IN HAND FCY (VAULT). 45
75 0075 CASH AT ATM 35
85 0085 CASH AT ATM 75
120 0120 BAL WITH BANGLADESH BANK 25
130 0130 BAL WITH BANGLADESH BANK LCY 120
135 0135 BAL. WITH BANGLADESH FCY 120
英文:

I have a ORACLE table data where data represents parent-child relation. But in my table there has no such relation. So how can I set parent-child relation in my table where no relation exists.

My table some sample data is given below for understanding.

ID CODE NAME PARENT_ID
5 0005 ROOT
15 0015 ASSETS
25 0025 CASH AND BANK BALANCE
35 0035 CASH IN HAND
45 0045 CASH IN VAULT
55 0055 CASH IN HAND LCY (VAULT)
60 0060 CASH IN HAND FCY (VAULT).
75 0075 CASH AT ATM
85 0085 CASH AT ATM
120 0120 BAL WITH BANGLADESH BANK
130 0130 BAL WITH BANGLADESH BANK LCY
135 0135 BAL. WITH BANGLADESH FCY

From above table, ROOT represents my table's data root. Under its ASSETS represent child of ROOT. Under the ASSETS, CASH AND BANK BALANCE is child. Under CASH AND BANK BALANCE, CASH IN HAND and BAL WITH BANGLADESH BANK are child. CASH IN VAULT and CASH AT ATM (0075) are child of CASH IN HAND. CASH IN HAND LCY (VAULT) and CASH IN HAND FCY (VAULT). are child of CASH IN VAULT. Similarly for CASH AT ATM and BAL WITH BANGLADESH BANK, CASH AT ATM (0085) and BAL WITH BANGLADESH BANK LCY and BAL. WITH BANGLADESH FCY are child respectively.

So from this data how can I make child-parent relational data.

My final data table would be like as following:

ID CODE NAME PARENT_ID
5 0005 ROOT
15 0015 ASSETS 5
25 0025 CASH AND BANK BALANCE 15
35 0035 CASH IN HAND 25
45 0045 CASH IN VAULT 35
55 0055 CASH IN HAND LCY (VAULT) 45
60 0060 CASH IN HAND FCY (VAULT). 45
75 0075 CASH AT ATM 35
85 0085 CASH AT ATM 75
120 0120 BAL WITH BANGLADESH BANK 25
130 0130 BAL WITH BANGLADESH BANK LCY 120
135 0135 BAL. WITH BANGLADESH FCY 120

答案1

得分: 2

这里没有明显的自动化方法(因为存在具有不同代码的重复名称),而是手动指定父子关系:

MERGE INTO table_name dst
USING (
  SELECT  15 AS id, 5 AS parent_id FROM DUAL UNION ALL
  SELECT  25,  15 FROM DUAL UNION ALL
  SELECT  35,  25 FROM DUAL UNION ALL
  SELECT  45,  35 FROM DUAL UNION ALL
  SELECT  55,  45 FROM DUAL UNION ALL
  SELECT  60,  45 FROM DUAL UNION ALL
  SELECT  75,  35 FROM DUAL UNION ALL
  SELECT  85,  75 FROM DUAL UNION ALL
  SELECT 120,  25 FROM DUAL UNION ALL
  SELECT 130, 120 FROM DUAL UNION ALL
  SELECT 135, 120 FROM DUAL
) src
ON (src.id = dst.id)
WHEN MATCHED THEN
  UPDATE SET parent_id = src.parent_id;
英文:

There is not an obvious method of automating this (as you have duplicate names with different codes); instead, manually specify the parent-child relationships:

MERGE INTO table_name dst
USING (
  SELECT  15 AS id, 5 AS parent_id FROM DUAL UNION ALL
  SELECT  25,  15 FROM DUAL UNION ALL
  SELECT  35,  25 FROM DUAL UNION ALL
  SELECT  45,  35 FROM DUAL UNION ALL
  SELECT  55,  45 FROM DUAL UNION ALL
  SELECT  60,  45 FROM DUAL UNION ALL
  SELECT  75,  35 FROM DUAL UNION ALL
  SELECT  85,  75 FROM DUAL UNION ALL
  SELECT 120,  25 FROM DUAL UNION ALL
  SELECT 130, 120 FROM DUAL UNION ALL
  SELECT 135, 120 FROM DUAL
) src
ON (src.id = dst.id)
WHEN MATCHED THEN
  UPDATE SET parent_id = src.parent_id;

huangapple
  • 本文由 发表于 2023年6月5日 14:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定