如何使用 PostgreSQL 更改层次结构树中的字符串输出?

tuc*_*max 2 postgresql cte recursive string-manipulation

我有一张这样的表:

-------------------------------------------------
|  id  | description         | parent_id   |  cost
--------------------------------------------------
| 1    |  Radiology         |       NULL  | 0.00
| 2    |  Lab Tests         |       NULL  | 0.00
| 3    |  Normal Radiology  |         1   | 0.00
| 4    |  Resonance         |         1   | 100.00
| 1100 |  Cerebral Resonance|         4   | 200.00
| 1900 |  Blood Tests       |         2   | 10.00
| 2044 |  Calcium           |         2   | 50.00

---------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我需要生成这种输出:

Radiology
   -->Normal Radiology
   -->Resonance
      -->Cerebral Resonance with contrast
Lab Test
    --> Blood Test
    --> Calcium
Run Code Online (Sandbox Code Playgroud)

我正在研究 PostgreSQL。我一直在用递归 CTE 尝试这个,但我无法生成我喜欢的:

WITH RECURSIVE hierarchy AS (
    SELECT  id, CAST(description AS TEXT) AS parent_list
    FROM    orders
    WHERE   parent_id is null
      UNION
    SELECT  c.id,
            CAST(c2.parent_list || ' --> ' || c.description as text) as parent_list
    FROM orders c
    INNER JOIN hierarchy c2 ON c.parent_id = c2.id )

SELECT  id, parent_list
FROM     hierarchy
GROUP BY id, parent_list
ORDER BY parent_list;
Run Code Online (Sandbox Code Playgroud)

该递归 CTE 产生以下不良输出:

Radiology
Radiology--> Normal Radiology
Radiology--> Resonance
Radiology--> Resonance --> Cerebral Resonance with contrast
Lab Test
Lab Test --> Blood Test
Lab Test --> Calcium
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

nbk*_*nbk 5

你可以使用这样的东西

CREATE TABLE orders (
  "id" INTEGER,
  "description" VARCHAR(18),
  "parent_id" VARCHAR(4),
  "cost" DECIMAL(8,2)
);

INSERT INTO orders
  ("id", "description", "parent_id", "cost")
VALUES
  ('1', 'Radiology', NULL, '0.00'),
  ('2', 'Lab Tests', NULL, '0.00'),
  ('3', 'Normal Radiology', '1', '0.00'),
  ('4', 'Resonance', '1', '100.00'),
  ('1100', 'Cerebral Resonance', '4', '200.00'),
  ('1900', 'Blood Tests', '2', '10.00'),
  ('2044', 'Calcium', '2', '50.00');
Run Code Online (Sandbox Code Playgroud)
WITH RECURSIVE hierarchy AS (
    SELECT  id, 1 AS rown, CAST(description AS TEXT) AS parent_list, id as parent
    FROM    orders
    WHERE   parent_id is null
    UNION  
    SELECT  c.id
    ,rown + 1 as rown
    ,CAST(repeat('    ', rown) || ' --> ' || c.description as text) as parent_list
    ,parent
    FROM orders c
    INNER JOIN hierarchy c2 ON CAST(c.parent_id AS INTEGER) = c2.id )
SELECT id,parent_list FROM hierarchy
ORDER BY parent DESC,rown
Run Code Online (Sandbox Code Playgroud)
  身份证 | 父列表                    
---: | :------------------------------
   2 | 实验室测试                      
1900 | --> 验血           
2044 | --> 钙               
   1 | 放射学                      
   4 | --> 共振             
   3 | --> 正常放射学      
1100 | --> 脑共振
WITH RECURSIVE hierarchy AS (
    SELECT  id, CAST(description AS TEXT) AS parent_list, 1 AS rown, id as parent
    FROM    orders
    WHERE   parent_id is null
    UNION  
    SELECT  c.id
    ,CAST(c2.parent_list || ' --> ' || c.description as text) as parent_list
    ,rown + 1 as rwon
    ,parent
    FROM orders c
    INNER JOIN hierarchy c2 ON CAST(c.parent_id AS INTEGER) = c2.id )
SELECT  id, parent_list
FROM    hierarchy
GROUP BY id, parent_list
ORDER BY parent_list;
Run Code Online (Sandbox Code Playgroud)
  身份证 | 父列表                                   
---: | :---------------------------------------------
   2 | 实验室测试                                     
1900 | 实验室测试 --> 血液测试                     
2044 | 实验室测试 --> 钙                         
   1 | 放射学                                     
   3 | 放射科 --> 正常放射科                
   4 | 放射学 --> 共振                       
1100 | 放射学 --> 共振 --> 脑共振

db<>在这里摆弄