我已经按照以下方式对所需的表进行了排序:
order by
case when name = 'zzz' then '1' else null end asc
case when name = 'aaa' then '2' else null end asc
case when name = 'rrr' then '3' else null end asc
...
...
现在,我想对我的表的当前顺序进行排序,得到这样的结果:
new_rank name
1 zzz
2 aaa
3 rrr
... ...
... ...
我尝试了row_number() over()
,但实际上我没有要排序的列。
答案 0 :(得分:0)
您在order by
声明中重复row_number()
:
select t.*,
row_number() over (order by (case when name = 'zzz' then 1 end) asc,
(case when name = 'aaa' then 2 end) asc,
(case when name = 'rrr' then 3 end) asc
) as seqnum
from . . .
注意:
else NULL
在case
表达式中是多余的。order by
,无论出现在何处,都应使用逗号分隔。并且,鉴于case
仅使用常量,您可能只使用一个:
select t.*,
row_number() over (order by (case when name = 'zzz' then 1
when name = 'aaa' then 2
when name = 'rrr' then 3
else 4
end)
) as seqnum
from . . .