我有一张这样的表
ID Subject StatusCd Date
------------------------------------------
1 abc 3 01/03/2013
2 def 1 01/03/2013
3 ghi 2 05/03/2013
4 jkl 3 02/03/2013
5 mno 3 06/03/2013
如何编写SQL查询,该查询将按StatusCd
(3,1,2)的顺序对行进行排序。
另外,我想根据Date
答案 0 :(得分:3)
您可以在CASE
子句中使用ORDER BY
表达式:
select id, subject, statuscd, date
from yt
order by
case statuscd
when 3 then 0
when 1 then 1
when 2 then 2
end, date
见SQL Fiddle with Demo。这给出了一个结果:
| ID | SUBJECT | STATUSCD | DATE |
----------------------------------------
| 1 | abc | 3 | 2013-01-03 |
| 4 | jkl | 3 | 2013-02-03 |
| 5 | mno | 3 | 2013-06-03 |
| 2 | def | 1 | 2013-01-03 |
| 3 | ghi | 2 | 2013-05-03 |
答案 1 :(得分:2)
请尝试:
select
ID,
[Subject],
StatusCd,
[Date]
from
YourTable
order by case StatusCd when 3 then 0 else StatusCd end,
[date]