条件订购

时间:2012-06-26 17:50:25

标签: sql sql-server tsql

我有以下样本数据。

AID    Date        Title
-----  ----------  ------
1      2011-12-12  test1
2      2011-12-12  test2
2      2011-12-12  test2
4      2011-12-12  test4
5      2011-12-12  test5
6      2011-12-13  test11
7      2011-12-13  test12
8      2011-12-13  test13
9      2011-12-13  test14
10     2011-12-13  test15
11     2011-12-14  test15
12     2011-12-14  test15

我需要像下面这样,意味着我想在第一个地方order by date,aid特定AID(5,10)。

AID    Date        Title
-----  ----------  ------
5      2011-12-12  test5
1      2011-12-12  test1
2      2011-12-12  test2
2      2011-12-12  test2
4      2011-12-12  test4
10     2011-12-13  test15
6      2011-12-13  test11
7      2011-12-13  test12
8      2011-12-13  test13
9      2011-12-13  test14
11     2011-12-14  test15
12     2011-12-14  test15

2 个答案:

答案 0 :(得分:5)

ORDER BY [Date], CASE WHEN AID IN (5,10) THEN 1 ELSE 2 END, AID;

答案 1 :(得分:1)

ORDER BY CASE WHEN (AID % 5) = 0 THEN (AID / 5 -1) * 5 ELSE  AID END