2017-03-17 118 views
1

我有一些SQL Server的麻煩。SQL Server ORDER BY - 排序排序方法

我在一列有這些號碼:

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

我想訂購這樣的:

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

有沒有人有什麼想法?

回答

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 | 
+--------+ 
+0

非常感謝..這工作.. – Marc

+0

@Marc快樂的幫助! – SqlZim

0

行數和partion查詢可以做到這一點。請通過它去..