Sree
Sree

Reputation: 584

Conditional ordering

I have below sample data.

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

I need to out put like below, means I want to particular AID(5,10) in first place order by date,aid.

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

Upvotes: 0

Views: 64

Answers (2)

EricZ
EricZ

Reputation: 6205

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

Upvotes: 1

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

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

Upvotes: 5

Related Questions