在两个相关列上排序表

问题描述 投票:0回答:3

我有一个这样的映射表

+------+----+
| From | To |
+------+----+
|   14 | 40 |
|   40 | 32 |
|   32 | 95 |
|   88 | 56 |
|   47 | 88 |
|   60 | 32 |
+------+----+

该表存储将相互映射的节点ID。

因此,从上表中,第一个14将被映射到40然后一些40和60的表达式将被映射到32,最后32个将被映射到95。

我想按照这样的映射顺序对此表进行排序

+------+----+-------+-------+
| From | To | Order | Group |
+------+----+-------+-------+
|   14 | 40 |     1 |     1 |
|   40 | 32 |     2 |     1 |
|   60 | 32 |     2 |     1 |
|   32 | 95 |     3 |     1 |
|   47 | 88 |     1 |     2 |
|   88 | 56 |     2 |     2 |
+------+----+-------+-------+

在此结果集中,顺序告诉使用映射的顺序。如果两行具有相同的顺序和组,则意味着两个from的表达式将映射到to。而group将单链映射组合在一起。

有没有办法使用SQL查询以这种方式对表进行排序?

sql oracle
3个回答
1
投票

你已经将你的请求扩展到多个链,根据你的解释,你来自最后并寻找所有前辈(因此得到60-32记录,因为你来自32-95)。

使用多个链,您还需要一个组密钥:

with cte (fromid, toid, groupkey, sortkey) as
(
    select 
      fromid, 
      toid,
      row_number() over (order by fromid) as groupkey,
      1 as sortkey
    from mytable
    where toid not in (select fromid from mytable)
    union all
    select
      m.fromid,
      m.toid,
      cte.groupkey,
      cte.sortkey - 1
    from mytable m
    join cte on cte.fromid = m.toid
)
select fromid, toid
from cte
order by groupkey, sortkey;

即使:z zxswい


0
投票

您需要递归查询。首先获取“To”中未出现“From”的所有记录,然后转到以下记录,依此类推。通过每一步,您都可以增加排序键。

http://rextester.com/AIAYA93175

0
投票

您可以利用递归函数。

看看with cte (fromid, toid, sortkey) as ( select fromid, toid, 1 as sortkey from mytable where fromid not in (select toid from mytable) union all select m.fromid, m.toid, cte.sortkey + 1 from mytable m join cte on cte.toid = m.fromid ) select fromid, toid from cte order by sortkey;

Recursive queries
 create table tbl (ffrom int, tto int);
insert into tbl values (40, 32);
insert into tbl values (32, 95);
insert into tbl values (14, 40);
FFROM | TTO
----: | --:
   14 |  40
   40 |  32
   32 |  95

dbfiddle select * from tbl start with ffrom = 14 connect by ffrom = prior tto

here
© www.soinside.com 2019 - 2024. All rights reserved.