|
|
我把上面有人提到的用case when的写法写出来,如newkid所说, 这种写法在字段数少的时候可以用一下, 如果涉及的字段数多,组合就多了,建议先简单合并, 再用一个自定义函数把字符串重新排序:
with tmp (c1,c2,c3,c4)
as
(select 'a','b','b','c' from dual union all
select 'b','b','a','d' from dual union all
select 'a','b','e','c' from dual union all
select 'e','b','a','d' from dual
),tmp1 as
(
select --sort high to low
case when c1>=c2 and c2>=c3 then c1||','||c2||','||c3
when c1>=c3 and c3>=c2 then c1||','||c3||','||c2
when c2>=c3 and c3>=c1 then c2||','||c3||','||c1
when c2>=c1 and c1>=c3 then c2||','||c1||','||c3
when c3>=c2 and c2>=c1 then c3||','||c2||','||c1
when c3>=c1 and c1>=c2 then c3||','||c1||','||c2
end as c1c2c3,
c1,c2,c3,c4
from tmp
)
select c1,c2,c3,c4 from
(
select a.* , row_number() over (partition by c1c2c3 order by null) as rn
from tmp1 a
)
where rn=1; |
|