VastbaseE100

基于开源技术的HTAP数据库管理系统。性能优异,稳定可靠,提供诸多专属领域特性。

Menu

CONNECT BY…… START WITH

CREATE TABLE sys_cbp_test2 (id INTEGER NOT NULL PRIMARY KEY,parent_id INTEGER );
INSERT INTO sys_cbp_test2 
SELECT 1, NULL FROM dual 
UNION ALL SELECT 2, 1 FROM dual
UNION ALL SELECT 3, 2 FROM dual 
UNION ALL SELECT 4, 3 FROM dual 
UNION ALL SELECT 5, 1 FROM dual 
UNION ALL SELECT 6, 5 FROM dual 
UNION ALL SELECT 7, 2 FROM dual 
UNION ALL SELECT 20, NULL FROM dual 
UNION ALL SELECT 21, 20 FROM dual 
UNION ALL SELECT 22, 21 FROM dual;

atlasdb=# SELECT id, parent_id,sys_connect_by_path (TO_CHAR (id), '/') AS Path
atlasdb-# FROM sys_cbp_test2 
atlasdb-# START WITH parent_id IS NULL CONNECT BY prior id = parent_id;
 id | parent_id |   path    
----+-----------+-----------
  1 |           | /1
 20 |           | /20
  2 |         1 | /1/2
  5 |         1 | /1/5
 21 |        20 | /20/21
  3 |         2 | /1/2/3
  6 |         5 | /1/5/6
  7 |         2 | /1/2/7
 22 |        21 | /20/21/22
  4 |         3 | /1/2/3/4
(10 rows)