我在我的sql上通过zoneId订购了
ZoneID | Description |
1 | something |
1 | something |
1 | something |
2 | something |
2 | something |
2 | something |
3 | something |
3 | something |
3 | something |
但是我想这样订购它,它可能
ZoneID | Description |
1 | something |
2 | something |
3 | something |
1 | something |
2 | something |
3 | something |
1 | something |
2 | something |
3 | something |
答案 0 :(得分:1)
在sql server中,您可以使用row_number()
select ZoneId, Description
from t
order by row_number() over (partition by ZoneId order by (select 1)), ZoneId
rextester演示:http://rextester.com/ATQBA77275
返回:
+--------+-------------+
| ZoneId | Description |
+--------+-------------+
| 1 | something |
| 2 | something |
| 3 | something |
| 1 | something |
| 2 | something |
| 3 | something |
| 1 | something |
| 2 | something |
| 3 | something |
+--------+-------------+
要在视图中使用它(order by
没有top
),只需添加row_number()
的列:
create view dbo.with_rownumber as
select
ZoneId
, Description
, rn = row_number() over (partition by ZoneId order by (select 1))
from t;
go
select ZoneId, Description
from dbo.with_rownumber
order by rn, ZoneId