以下是在PL / SQL-
中实现的要求表格格式为
CREATE TABLE NETWORK_TABLE ( ORIG_CODE NUMBER, ORIG_SV NUMBER, DEST_CODE NUMBER, DEST_SV NUMBER )
示例数据 -
INSERT INTO network_table VALUES ( 14, 1, 15, 1);
INSERT INTO network_table VALUES ( 18, 4, 11, 1);
INSERT INTO network_table VALUES ( 15, 1, 22, 3);
INSERT INTO network_table VALUES ( 23, 2, 21, 1);
INSERT INTO network_table VALUES ( 14, 3, 11, 1);
INSERT INTO network_table VALUES ( 12, 2, 22, 2);
表数据看起来像 -
Orig_r orig_sv dest_r dest_sv
14 1 15 1
12 2 22 2
18 4 11 1
15 1 22 3
14 3 11 1
现在,我想得到如下输出 -
ROOT SV
14 1
15 1
12 2
22 2
18 4
11 1
15 1
22 3
14 3
1 1
我怎样才能实现这一目标?感谢您的意见
答案 0 :(得分:2)
SQL> select * from network_table;
ORIG_CODE ORIG_SV DEST_CODE DEST_SV
---------- ---------- ---------- ----------
14 1 15 1
12 2 22 2
18 4 11 1
15 1 22 3
14 3 11 1
SQL> select case name when 'ORIG_SV' then orig_code else dest_code end code, val
2 from network_table
3 unpivot (val for name in (orig_sv, dest_sv));
CODE VAL
---------- ----------
14 1
15 1
12 2
22 2
18 4
11 1
15 1
22 3
14 3
11 1
或10g及以下:
SQL> select case r when 1 then orig_code else dest_code end code,
2 case r when 1 then orig_sv else dest_sv end val
3 from network_table, (select rownum r from dual connect by level <= 2)
4 /
CODE VAL
---------- ----------
14 1
12 2
18 4
15 1
14 3
15 1
22 2
11 1
22 3
11 1
答案 1 :(得分:0)
您可以使用UNION ALL
将数据从列转换为行。:
select ORIG_CODE Root, ORIG_SV SV
from network_table
union all
select DEST_CODE, DEST_SV
from network_table
order by root
UNION ALL
会留下任何重复的值。如果要删除重复项,则可以使用UNION