日期:2014-05-17 浏览次数:20553 次
--创建临时表
if object_id('Tempdb..#t') is not null drop table #t
create table #t(
id int identity(1,1) not null,
brand nvarchar(100) null,
model nvarchar(100) null,
sales int null
)
--插入数据
Insert Into #t
select 'A','A1',101 union all
select 'A','A2',122 union all
select 'A','A3',113 union all
select 'A','A4',104 union all
select 'A','A5',95 union all
select 'B','B1',121 union all
select 'B','B2',112 union all
select 'B','B3',103 union all
select 'B','B4',131 union all
select 'C','C1',112 union all
select 'C','C2',122 union all
select 'C','C3',103 union all
select 'C','C4',124 union all
select 'D','D1',133 union all
select 'D','D2',131 union all
select 'D','D3',135 union all
select 'E','E1',132 union all
select 'E','E2',133
--查询
select top 5 *, row_number() over(partition by brand order by sales desc) as orderid
from #t
order by orderid,sales desc
-------------------------
--查询结果
(18 行受影响)
id brand model &nbs