我有一个具有以下结构的表
import UI_simple
from PyQt5 import QtWidgets, QtCore
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = UI_simple.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(QtWidgets.QApplication.quit)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Child_id
Parent_Id
Child_name
Parent_name
我希望查询将所有叶级节点的所有父级都放在一行中。
例如:如果Child_Description
和X
是以下叶子级节点:
Y
查询应返回如下两行
A->B->C->D->X
F->G->H->I->Y
谢谢, 开发人员
答案 0 :(得分:0)
这有意义吗?
SQL> with test (child_id, parent_id) as
2 (select 'x', 'd' from dual union all
3 select 'd', 'c' from dual union all
4 select 'c', 'b' from dual union all
5 select 'b', 'a' from dual union all
6 select 'a', null from dual union all
7 --
8 select 'y', 'i' from dual union all
9 select 'i', 'h' from dual union all
10 select 'h', 'g' from dual union all
11 select 'g', 'f' from dual union all
12 select 'f', null from dual
13 ),
14 anc as
15 (select sys_connect_by_path(child_Id, '>') pth
16 from test
17 where connect_by_isleaf = 1
18 connect by prior child_id = parent_id
19 start with parent_id is null
20 )
21 select regexp_substr(pth, '[^>]+', 1, 5) c1,
22 regexp_substr(pth, '[^>]+', 1, 4) c2,
23 regexp_substr(pth, '[^>]+', 1, 3) c3,
24 regexp_substr(pth, '[^>]+', 1, 2) c4,
25 regexp_substr(pth, '[^>]+', 1, 1) c5
26 from anc;
C1 C2 C3 C4 C5
-- -- -- -- --
x d c b a
y i h g f
SQL>
它有什么作用?
test
CTE模拟您的数据(至少,我认为是这样)anc
(estors)CTE选择“最长”路径,因为CONNECT_BY_ISLEAF
显示当前行是否可以(或不能)进一步扩展。如果返回1
,则不会。>
;可以是其他字符串)转换为列。它没有动态变化,因此-如果您拥有的数据与最多5个“列”不同,则必须对其进行修复