- How to
ORDER BY
with a CASE
-Statement
- 第一组:日期列中的空值
Col1
按日期列排序Col2 DESC
- 第二组:日期列中的非空值
Col1
排序Col1 DESC
我尝试过以下操作:
SELECT columns FROM tables WHERE condition
ORDER BY
case when Table1.Col1 IS NULL then 0 end, Table2.Col2 DESC,
case when Table1.Col1 IS NOT NULL then 1 end, Table1.Col1 DESC
但排序顺序错误,NOT NULL 值排在第一位(按 Col2 而不是 Col1 排序)。我想我错过了一个细节。
SELECT columns FROM tables
WHERE condition
ORDER BY
case when Table1.Col1 IS NULL then 0 else 1 end ASC
,case when Table1.Col1 IS NULL then Table2.Col2 else Table1.Col1 end DESC
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)