sql2005-ranking函数row_number() rank() dense_rank()

drop table tb_1;
go
Create Table [Dbo].[Tb_1](
    [Id] [Int] Not Null,
    [Name] [Char](20) Not Null,
    [Age] [Smallint] Not Null,
    [Addr] [Char](40) Not Null,
    [Val] [Int] Not Null,
    Constraint [Pk_Tb_1] Primary Key Clustered ( [Id] Asc )
)
go

insert into tb_1 values(1,'aaaa1',20,'sz111',2000);
insert into tb_1 values(2,'aaaa2',22,'sz111',3300);
insert into tb_1 values(3,'aaaa3',23,'sz111',2200);
insert into tb_1 values(4,'aaaa4',24,'sz111',2200);
insert into tb_1 values(5,'aaaa5',25,'sz112',1600);

select * from tb_1;


--顺序排名 1 2 3 4 5 ,cnt列是由row_number函数生成的序号列
select id,name,val, row_number() over(order by val desc) cnt
from tb_1;
 
--并列排名,序号不连续   1 2 3 3 5
select id,name,val,
rank()over(order by val) cnt
from tb_1;
--并列排名,序号连续    1 2 3 3 4
select id,name,val,
dense_rank() over(order by val) cnt
from tb_1;

你可能感兴趣的:(sql2005-ranking函数row_number() rank() dense_rank())