需求目标,代码值:2,5,1,0按照这个顺序
本来是没有5
按照21,null,0的顺序
可以将要排序的代码使用NVL转化null为0进行排序
不过这里用不了,我这是使用case when语句,自定义一个字段进行排序
select
fslqgzxz.C_ID,
fslqgzxz.C_ND,
fslqgzxz.C_PC,
fslqgzxz.C_BZ,
fslqgzxz.C_SPZT,
fslqgzxz.C_SPJG,
fslqgzxz.C_FJXX,
fslqgzxz.C_SPYJ,
YX.yxmc as yxmc,
(case
when fslqgzxz.C_SPZT = '2' then
'1'
when fslqgzxz.C_SPZT = '5' then
'2'
when fslqgzxz.C_SPZT = '1' then
'3'
when fslqgzxz.C_SPZT = '0' then
'4'
else
'5'
end) as orderNum
from T_ZSGL_FSLQGZXZ fslqgzxz
LEFT JOIN LY_YJS_HXSJ.T_GGZY_DWXX DWXX
ON DWXX.ZSDWDM = fslqgzxz.C_YXDM
LEFT JOIN (select distinct A.C_YXDM as id,
A.C_YXDM as yxdm,
A.C_YXMC as yxmc,
A.C_ND as nd
from T_ZSGL_YXZYK A
order by A.C_YXDM ASC) YX
ON YX.yxdm = fslqgzxz.C_YXDM
AND YX.nd = fslqgzxz.C_ND
WHERE C_ND = '2024'
order by orderNum asc
这是运行结果:
你可以将想要的代码写在CASE WHEN语句中来决定顺序
我这里用的是具体值,你也可以使用区间定义
1-3 then 1
2-4 then 2
这种方式来自定义