- --生成OrderTest
- DROP TABLE OrderTest
- WITH TEST(N, VAL)
- AS
- (
- SELECT 1 N
- ,CAST('A' AS VARCHAR) VAL
- UNION ALL
- SELECT N+1
- ,CAST(SUBSTRING('ABCDE', N+1, 1) AS VARCHAR)
- FROM TEST
- WHERE N<5
- )
- SELECT * INTO OrderTest
- FROM TEST
- --排序12354
- SELECT N
- FROM OrderTest
- ORDER BY --越小越前面
- --區域前 (123)(45)
- CASE WHEN n>3 THEN 1 ELSE 0 END
- --區預後 (123)(54)
- CASE WHEN n>3 THEN 0-N ELSE N END
- --CBADE
- SELECT val
- FROM OrderTest
- ORDER BY
- --分類(abc)(de)
- CASE WHEN n>3 THEN 1 ELSE 0 END
- --到轉(cba)(de)
- ,CASE WHEN n<4 THEN 0-N ELSE N END
- --cbade
- SELECT val
- FROM OrderTest
- ORDER BY
- --分兩類
- --case when val in ('D','E') THEN 1 ELSE 0 END
- --排序
- CASE WHEN val < 'D' THEN
- ROW_NUMBER() OVER(ORDER BY val DESC)
- ELSE ROW_NUMBER() OVER(ORDER BY val ASC) END
2014年5月13日 星期二
[SQL] order by case 排序
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言