SQL 连接多行

发布于 2024-10-16 01:36:08 字数 715 浏览 2 评论 0原文

是否可以为表中的许多行生成“连接方式”并对它们进行求和。 我有一张桌子

person boss
---------------    
person1 NULL
person2 person1
person3 person2

,我想要得到一张桌子,

boss is_boss_of
---------------
person1 person2
person1 person3
person2 person3

我想通过表达式为桌子上的每个人建立连接? 有什么办法可以做到吗?

说到总和,我想到了这样的事情

SELECT CONNECT_BY_ROOT person as boss, person as is_boss_of
            FROM table1
            START WITH boss = person1
            CONNECT BY PRIOR Empno = Mgr;
Union
SELECT CONNECT_BY_ROOT person as boss, person as is_boss_of
            FROM table1
            START WITH boss = person2
            CONNECT BY PRIOR Empno = Mgr;
Union
...
and so on

Is it possible to generate "connect by" for many rows in table and sum them all.
I have a table

person boss
---------------    
person1 NULL
person2 person1
person3 person2

And i want to get table

boss is_boss_of
---------------
person1 person2
person1 person3
person2 person3

I would like to make connect by expression for everyone in table person-boss?
Is there any way to make it?

Saying about sum i thought about something like this

SELECT CONNECT_BY_ROOT person as boss, person as is_boss_of
            FROM table1
            START WITH boss = person1
            CONNECT BY PRIOR Empno = Mgr;
Union
SELECT CONNECT_BY_ROOT person as boss, person as is_boss_of
            FROM table1
            START WITH boss = person2
            CONNECT BY PRIOR Empno = Mgr;
Union
...
and so on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-10-23 01:36:08

更新

看到更新后,您只需删除“开始”即可。

结束更新

CONNECT_BY_ROOT< /a> 是您要查找的内容

此语句

SELECT distinct RootBoss, Person FROM (
with employee  as 
(
  Select 1 person , null boss from Dual
  UNION Select 2 , 1 from dual
  UNION Select 3 , 2 from dual
)
SELECT CONNECT_BY_ROOT boss RootBoss, person
FROM employee connect_by

  connect by prior person = boss
  ORDER BY person

  ) t
WHERE ROOTBOSS is not null
ORDER BY
RootBoss, Person

输出

ROOTBOSS               PERSON                 
---------------------- ---------------------- 
1                      2                      
1                      3                      
2                      3        

添加 SUMS 和 GROUPS 非常简单

Update

After seeing your update, you just need to drop the START WITH.

End update

CONNECT_BY_ROOT is what you're looking for

This statement

SELECT distinct RootBoss, Person FROM (
with employee  as 
(
  Select 1 person , null boss from Dual
  UNION Select 2 , 1 from dual
  UNION Select 3 , 2 from dual
)
SELECT CONNECT_BY_ROOT boss RootBoss, person
FROM employee connect_by

  connect by prior person = boss
  ORDER BY person

  ) t
WHERE ROOTBOSS is not null
ORDER BY
RootBoss, Person

Outputs

ROOTBOSS               PERSON                 
---------------------- ---------------------- 
1                      2                      
1                      3                      
2                      3        

Adding SUMS and GROUPS is pretty easy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文