为什么结果是这样?和预期有点不一样

qwx312347236 2008-11-15 01:59:37
脚本:
/* Formatted on 2008/11/15 11:10 (Formatter Plus v4.8.6) */
SELECT LPAD(' ', 5*(LEVEL - 1))||son son, LPAD(' ', 5*(LEVEL - 1))||dad dad
FROM qwx.visit_tree
START WITH son = 'A'
CONNECT BY dad = PRIOR son;

运行结果:
SON
--------------------------------------------------------------------------------
DAD
--------------------------------------------------------------------------------
A
root

B
A

E
B

F
B

H
F

I
F

C

SON
--------------------------------------------------------------------------------
DAD
--------------------------------------------------------------------------------
A

G
C

J
G

K
G

L
G

D
A


12 rows selected.
...全文
53 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwx312347236 2008-11-15
  • 打赏
  • 举报
回复
是显示效果未达到预期
qwx312347236 2008-11-15
  • 打赏
  • 举报
回复
我的预期结果是这样
SON DAD ------------------------- ------------------------
A root

B A

E B

F B

H F
I F
C A
G C
J G
K G
L G
D A
BlueskyWide 2008-11-15
  • 打赏
  • 举报
回复

--在网友原文(http://topic.csdn.net/t/20020711/11/866042.html)的基础上作了些修改,参考一下:

SQL> drop table test_tree;

Table dropped

SQL> create table test_tree
2 (
3 userid number(8) not null primary key,
4 username varchar2(18) not null,
5 p_username varchar2(18),
6 num number(10,2)
7 );

Table created

SQL> insert into test_tree values(1,'1000',null,null);

1 row inserted

SQL> insert into test_tree values(2,'1100','1000',null);

1 row inserted

SQL> insert into test_tree values(3,'1200','1000',null);

1 row inserted

SQL> insert into test_tree values(4,'1101','1100',100);

1 row inserted

SQL> insert into test_tree values(5,'1102','1100',200);

1 row inserted

SQL> insert into test_tree values(6,'1104','1100',250);

1 row inserted

SQL> insert into test_tree values(7,'1201','1200',150);

1 row inserted

SQL> insert into test_tree values(8,'1205','1200',200);

1 row inserted

SQL> insert into test_tree values(9,'1207','1200',150);

1 row inserted

SQL> commit;

Commit complete

SQL> select * from test_tree;

USERID USERNAME P_USERNAME NUM
--------- ------------------ ------------------ ------------
1 1000
2 1100 1000
3 1200 1000
4 1101 1100 100.00
5 1102 1100 200.00
6 1104 1100 250.00
7 1201 1200 150.00
8 1205 1200 200.00
9 1207 1200 150.00

9 rows selected

SQL> select b.username,
2 b.p_username,
3 (select distinct sum(nvl(a.num, 0))
4 from test_tree a
5 start with a.username = b.username
6 connect by a.p_username = prior a.username) sum
7 from test_tree b
8 start with username = '1000'
9 connect by p_username = prior username;

USERNAME P_USERNAME SUM
------------------ ------------------ ----------
1000 1050
1100 1000 550
1101 1100 100
1102 1100 200
1104 1100 250
1200 1000 500
1201 1200 150
1205 1200 200
1207 1200 150

9 rows selected

SQL>




[Quote=引用楼主 qwx312347236 的帖子:]
脚本:
/* Formatted on 2008/11/15 11:10 (Formatter Plus v4.8.6) */
SELECT LPAD(' ', 5*(LEVEL - 1))||son son, LPAD(' ', 5*(LEVEL - 1))||dad dad
FROM qwx.visit_tree
START WITH son = 'A'
CONNECT BY dad = PRIOR son;

运行结果:
SON
------------------------------------------------------------------------…
[/Quote]

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧