我有两列,想查询类似A-> B-> A-> B-> A ......直到结束。在一个位置A没有B即结束。请指导如何查询以获得结果。
A B
----------------------------------------------------------------------
{07906439-7636-462D-95AE-B0D7683814A8} {69DA38DB-BA4F-4F34-9DCB-4F1DF7C353FD}
{69DA38DB-BA4F-4F34-9DCB-4F1DF7C353FD} {0460261B-833E-4FCD-981B-26A7846B593D}
{0460261B-833E-4FCD-981B-26A7846B593D} {713607FA-32ED-4AFD-83AF-5CA346A1A019}
{713607FA-32ED-4AFD-83AF-5CA346A1A019}
答案 0 :(得分:3)
在下面的查询中,您将0,2,4 ...分配给A列,将1,3,5 ...分配给B列,然后您只需union all
列具有这些等级并按其排序,你会实现交替记录的方式。请注意,我使用order by (select null)
,这是表的物理顺序,您可能想要引入一些保证的列,结果将是不变的。
select * from (
select [A],row_number() over (order by (select null))*2-2 [rn] from MY_TABLE
union all
select [B],row_number() over (order by (select null))*2-1 [rn] from MY_TABLE
) [a] order by [rn]
答案 1 :(得分:1)
尝试递归CTE ......类似于以下内容
(assoc order :state "deleted")