Oracle 查询所有树节点的包含的子节点的数量 30
表结构CreateTable(IDint,PIDint,NameVarchar(20),primaryKey(ID));数据IDPIDName1-1A21B31C42D5...
表结构
Create Table( ID int, PID int , Name Varchar(20), primary Key(ID));
数据
ID PID Name
1 -1 A
2 1 B
3 1 C
4 2 D
5 2 E
6 3 F
7 4 G
希望得到的结果:
Name Count
A 7 (A~G)
B 4 (B, D, G, E)
C 2 (C, F)
D 2 (D, G)
E 1
F 1
G 1 展开
Create Table( ID int, PID int , Name Varchar(20), primary Key(ID));
数据
ID PID Name
1 -1 A
2 1 B
3 1 C
4 2 D
5 2 E
6 3 F
7 4 G
希望得到的结果:
Name Count
A 7 (A~G)
B 4 (B, D, G, E)
C 2 (C, F)
D 2 (D, G)
E 1
F 1
G 1 展开
1个回答
展开全部
可能一个查询语句是完成不了你这个东西了。
DECLARE
CURSOR V_CURSOR IS
SELECT * FROM TABLE_TEST;
V_COUNT NUMBER;
BEGIN
FOR C1 IN V_CURSOR LOOP
SELECT COUNT(1)
INTO V_COUNT
FROM TABLE_TEST A
START WITH A.ID = C1.ID
CONNECT BY PRIOR A.ID = A.PID;
DBMS_OUTPUT.PUT_LINE(C1.NAME || ',' || V_COUNT);
END LOOP;
END;
DECLARE
CURSOR V_CURSOR IS
SELECT * FROM TABLE_TEST;
V_COUNT NUMBER;
BEGIN
FOR C1 IN V_CURSOR LOOP
SELECT COUNT(1)
INTO V_COUNT
FROM TABLE_TEST A
START WITH A.ID = C1.ID
CONNECT BY PRIOR A.ID = A.PID;
DBMS_OUTPUT.PUT_LINE(C1.NAME || ',' || V_COUNT);
END LOOP;
END;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询