我想按以下顺序订购以下文字,但在尝试以下查询后,它无效。
要订购的值是" A"," B"," Y"," Z"," a&#34 ;," b"," y"," z"。
预期结果" ZzYyBbAa"
SELECT COL FROM TABLE ORDER BY COL DESC;
SELECT COL FROM TABLE ORDER BY UPPER/LOWER(COL) DESC; Result-> ZzYybBaA
SELECT COL FROM TABLE ORDER BY NLS_UPPER/NLS_LOWER(COL) DESC; Result-> ZzYybBaA
答案 0 :(得分:9)
首先,您可以通过列的UPPER(或LOWER)情况进行排序,但是一旦您完成了该操作,您需要按文本本身排序以获得首字母的订单;例如:
with sample_data as (select 'A' txt from dual union all
select 'B' txt from dual union all
select 'Y' txt from dual union all
select 'Z' txt from dual union all
select 'a' txt from dual union all
select 'b' txt from dual union all
select 'y' txt from dual union all
select 'z' txt from dual)
select txt
from sample_data
order by upper(txt) desc, txt;
TXT
---
Z
z
Y
y
B
b
A
a
答案 1 :(得分:2)
另一种选择是使用语言排序:
SELECT COL FROM TABLE ORDER BY NLSSORT(COL, 'NLS_SORT=GENERIC_M') DESC
输出
Z
z
Y
y
B
b
A
a