group multiple oracle tables in one table based on order

若如初见. 提交于 2021-02-11 15:31:33

问题


I'm new to oracle. I have three tables: START_NODES:

ref_id    id     orn
--------------------
1         100    0
1         200    1
1         300    2
1         400    3
2         160    0
2         260    1
2         360    2
2         460    3

Middle_NODES:

ref_id    id     orn
--------------------
1        1000    0
1        2000    1
2        1500    0
2        2500    1
2        3500    2
2        4500    3
2        5500    4

END_NODES:

ref_id    id     orn
--------------------
1         150    0
1         250    1
1         350    2
1         450    3
2         170    0
2         270    1
2         370    2
2         470    3

I need to group them in one table based on ref_id and orn where orn is the order of start_nodes then middle nodes then end_nodes :

START_NODES:

ref_id    id     orn
--------------------
1         100     0
1         200     1
1         300     2
1         400     3
1         1000    4
1         2000    5
1         150     6
1         250     7
1         350     8
1         450     9
2         160     0
2         260     1
2         360     2
2         460     3
2         1500    4
2         2500    5
2         3500    6
2         4500    7
2         5500    8
2         170     9
2         270     10
2         370     11
2         470     12

any hint how to start?


回答1:


You can achieve it using the UNION ALL as follows

SELECT REF_ID, ID, ROW_NUMBER() OVER (PARTITION BY REF_ID ORDER BY NOD, ORN) - 1 AS ORN
FROM
(SELECT T.*, 1 AS NOD FROM START_NODES T
UNION ALL
SELECT T.*, 2 AS NOD FROM MIDDLE_NODES T
UNION ALL
SELECT T.*, 3 AS NOD FROM END_NODES T)



回答2:


Try joining selects on the table such as this:

SELECT  1 table_id,
        ref_id,
        id,
        orn
FROM    start_nodes

Order the join as you need. Use this table as an inner query to the query on the desired columns.



来源:https://stackoverflow.com/questions/60992834/group-multiple-oracle-tables-in-one-table-based-on-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!