如何同时使用 oracle distinct 和 order siblings by ?
How to using oracle distinct and order siblings by at the same time?
distinct 打乱了兄弟姐妹的顺序,我怎样才能同时使用它们?
如
select distinct * from table xxx starts with ... connect by id=pid 按字段排序兄弟姐妹
这是用不同结果执行的测试 sqls
select distinct * from test_table start with pid is null connect by prior id=pid order siblings by order_num;
select * from test_table start with pid is null connect by prior id=pid order siblings by order_num;
这里是 table
create table TEST_TABLE(id NUMBER,pid NUMBER,order_num NUMBER);
insert into TEST_TABLE (id, pid, order_num) values (1, null, 1);
insert into TEST_TABLE (id, pid, order_num) values (2, 1, 5);
insert into TEST_TABLE (id, pid, order_num) values (3, null, 2);
insert into TEST_TABLE (id, pid, order_num) values (4, 1, 4);
insert into TEST_TABLE (id, pid, order_num) values (5, 3, 2);
您可以在内部查询中使用 ORDER SIBLINGS BY
,然后使用 ROW_NUMBER()
分析函数在外部查询中查找重复项并在该外部查询中使用 ORDER BY ROWNUM
维护顺序:
SELECT id, pid, order_num
FROM (
SELECT id, pid, order_num,
ROW_NUMBER() OVER (PARTITION BY id, pid, order_num ORDER BY ROWNUM) AS rn
FROM (
SELECT id, pid, order_num
FROM test_table
START WITH pid IS NULL
CONNECT BY PRIOR id = pid
ORDER SIBLINGS BY order_num
)
ORDER BY ROWNUM
)
WHERE rn = 1
其中,对于示例数据:
create table TEST_TABLE(id,pid,order_num) AS
SELECT 1, NULL, 1 FROM DUAL UNION ALL
SELECT 2, 1, 5 FROM DUAL UNION ALL
SELECT 3, NULL, 2 FROM DUAL UNION ALL
SELECT 4, 1, 4 FROM DUAL UNION ALL
SELECT 5, 3, 2 FROM DUAL UNION ALL
SELECT 1, 5, 5 FROM DUAL;
注意:这有一个添加的行,因此有一条路径返回到层次结构中的前一个分支以在输出中创建重复的行。
输出:
ID
PID
ORDER_NUM
1
null
1
4
1
4
2
1
5
3
null
2
5
3
2
1
5
5
并维护层次查询中兄弟姐妹的顺序。
db<>fiddle here
distinct 打乱了兄弟姐妹的顺序,我怎样才能同时使用它们? 如 select distinct * from table xxx starts with ... connect by id=pid 按字段排序兄弟姐妹 这是用不同结果执行的测试 sqls
select distinct * from test_table start with pid is null connect by prior id=pid order siblings by order_num;
select * from test_table start with pid is null connect by prior id=pid order siblings by order_num;
这里是 table
create table TEST_TABLE(id NUMBER,pid NUMBER,order_num NUMBER);
insert into TEST_TABLE (id, pid, order_num) values (1, null, 1);
insert into TEST_TABLE (id, pid, order_num) values (2, 1, 5);
insert into TEST_TABLE (id, pid, order_num) values (3, null, 2);
insert into TEST_TABLE (id, pid, order_num) values (4, 1, 4);
insert into TEST_TABLE (id, pid, order_num) values (5, 3, 2);
您可以在内部查询中使用 ORDER SIBLINGS BY
,然后使用 ROW_NUMBER()
分析函数在外部查询中查找重复项并在该外部查询中使用 ORDER BY ROWNUM
维护顺序:
SELECT id, pid, order_num
FROM (
SELECT id, pid, order_num,
ROW_NUMBER() OVER (PARTITION BY id, pid, order_num ORDER BY ROWNUM) AS rn
FROM (
SELECT id, pid, order_num
FROM test_table
START WITH pid IS NULL
CONNECT BY PRIOR id = pid
ORDER SIBLINGS BY order_num
)
ORDER BY ROWNUM
)
WHERE rn = 1
其中,对于示例数据:
create table TEST_TABLE(id,pid,order_num) AS
SELECT 1, NULL, 1 FROM DUAL UNION ALL
SELECT 2, 1, 5 FROM DUAL UNION ALL
SELECT 3, NULL, 2 FROM DUAL UNION ALL
SELECT 4, 1, 4 FROM DUAL UNION ALL
SELECT 5, 3, 2 FROM DUAL UNION ALL
SELECT 1, 5, 5 FROM DUAL;
注意:这有一个添加的行,因此有一条路径返回到层次结构中的前一个分支以在输出中创建重复的行。
输出:
ID PID ORDER_NUM 1 null 1 4 1 4 2 1 5 3 null 2 5 3 2 1 5 5
并维护层次查询中兄弟姐妹的顺序。
db<>fiddle here