问题
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