SQL Server ORDER BY - 排序顺序方法

时间:2017-03-17 17:09:32

标签: sql methods numbers sql-order-by

我在列中列出了这些数字:

|numbers
|1
|1
|1
|1
|2
|2
|2
|3

我想点这样订购:

|numbers
|1
|2
|3
|1
|2
|1
|2
|1

有没有人有任何想法?

2 个答案:

答案 0 :(得分:2)

在子查询中使用 row_number()

select number 
from (
  select number
    , rn = row_number() over (
             partition by number 
             order by (select 1)
           )
  from t
) as s
order by rn, number

rextester演示:http://rextester.com/NNGS34159

结果

+--------+
| number |
+--------+
|      1 |
|      2 |
|      3 |
|      1 |
|      2 |
|      1 |
|      2 |
|      1 |
+--------+

答案 1 :(得分:0)

行计数和分区查询可以做到这一点..请仔细阅读..