ORACLE 按条件排序的例子技巧
刚看了一篇关于ORACLE按条件排序的例子 的blog,发现对以后的业务有帮助,特注意一下:
需求: 当天约会的案件在最前面--> 从未处理过的案件--> 上次处理时间由小到大排序(即越近催收的案件在越后面)
即:(col_case.case_app_date=sysdate)-->(col_case.case_lst_col_date is null)-->(col_case.case_lst_col_date)
SELECT
CASE
WHEN TRUNC(t.case_app_date)=TRUNC(sysdate)
THEN '1'
WHEN t.case_lst_col_date_str IS NULL
THEN '2'
ELSE '3'
END AS column1,
t.*
FROM
(SELECT V_COLOVERVIEW_FULL.*
FROM V_COLOVERVIEW_FULL
WHERE MCOL_ID ='zhangq'
OR(AGENT_ID ='zhangq'
AND AGENT_STATUS ='AgentStatus_Agree'
AND AGENT_DATE_FROM<=SYSDATE
AND AGENT_DATE_TO >=SYSDATE)
) t
ORDER BY column1,
case_lst_col_date_str ASC
CASE
WHEN TRUNC(t.case_app_date)=TRUNC(sysdate)
THEN '1'
WHEN t.case_lst_col_date_str IS NULL
THEN '2'
ELSE '3'
END AS column1,
t.*
FROM
(SELECT V_COLOVERVIEW_FULL.*
FROM V_COLOVERVIEW_FULL
WHERE MCOL_ID ='zhangq'
OR(AGENT_ID ='zhangq'
AND AGENT_STATUS ='AgentStatus_Agree'
AND AGENT_DATE_FROM<=SYSDATE
AND AGENT_DATE_TO >=SYSDATE)
) t
ORDER BY column1,
case_lst_col_date_str ASC
注明:
这里的排序技巧,主要是在case上做了一个判断。来保证数据排序的正确性。