0

我有一张这样的桌子

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

StatusCd我如何编写一个 SQL 查询,它将按照(3, 1, 2)的顺序对行进行排序。此外,我想根据其对组中的每一行进行排序Date

4

2 回答 2

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 |
于 2013-04-04T12:03:06.057 回答
2

请试试:

select 
    ID, 
    [Subject], 
    StatusCd, 
    [Date] 
from
    YourTable
order by case StatusCd when 3 then 0 else StatusCd end, 
    [date]
于 2013-04-04T12:14:30.453 回答