如何仅保留两列的值的一种组合?
我有一个表(查询结果),像:
ID1 |ID2 |SomeNotImportantComun
1 |2 |A
2 |1 |A
3 |4 |C
对于第1行和第2行,ID1和ID2的组合是相同的[1,2]。我想只保留其中一行。
ID1 |ID2 |SomeNotImportantComun
2 |1 |A
3 |4 |C
我怎样才能做到这一点?
您可以尝试使用以下方法:
with test(ID1, ID2, SomeNotImportantComun) as
(
select 1 ,2, 'A' from dual union all
select 2 ,1, 'A' from dual union all
select 3 ,4, 'C' from dual
)
select id1, id2, SomeNotImportantComun
from (
select id1, id2, SomeNotImportantComun,
row_number() over ( partition by least(id1, id2), greatest(id1, id2) order by id1 desc, id2 desc) as rank
from test
)
where rank = 1
在这里,对于ID的任何组合,您只保留一行;您可以通过编辑该order by
子句来决定要保留哪一个
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句