排名函数 RANK,NTILE,DENSE_RANK,ROW_NUMBER
表##Product 数据:
表##ProductType 数据:
得到结果:
全部代码:
create table ##Product
(
id int,
ProductName nvarchar(20),
ProductType int
)
Create table ##ProductType(
id int,
description nvarchar(200)
)
insert into ##Product values (1,N'C#入门经典',1)
insert into ##Product values (2,N'C#高级编程',1)
insert into ##Product values (3,N'C#技术内幕',1)
insert into ##Product values (4,N'N70',2)
insert into ##Product values (5,N'N73',2)
insert into ##Product values (6,N'N95',2)
insert into ##ProductType values (1,N'图书')
insert into ##ProductType values (2,N'手机')
select * from
(SELECT a.id, a.ProductName,b.description,
RANK() OVER (PARTITION BY a.ProductType order by a.id desc) as RANK
FROM ##Product a join ##ProductType b on a.ProductType=b.id
) c
where c.RANK < 3
(
id int,
ProductName nvarchar(20),
ProductType int
)
Create table ##ProductType(
id int,
description nvarchar(200)
)
insert into ##Product values (1,N'C#入门经典',1)
insert into ##Product values (2,N'C#高级编程',1)
insert into ##Product values (3,N'C#技术内幕',1)
insert into ##Product values (4,N'N70',2)
insert into ##Product values (5,N'N73',2)
insert into ##Product values (6,N'N95',2)
insert into ##ProductType values (1,N'图书')
insert into ##ProductType values (2,N'手机')
select * from
(SELECT a.id, a.ProductName,b.description,
RANK() OVER (PARTITION BY a.ProductType order by a.id desc) as RANK
FROM ##Product a join ##ProductType b on a.ProductType=b.id
) c
where c.RANK < 3
posted on 2009-06-16 10:43 zengshunyou 阅读(186) 评论(0) 编辑 收藏 举报