CREATE TABLE [dbo].[marks](
[student_id] [int] IDENTITY(1,1) NOT NULL,
[score] [int] NULL
) ON [PRIMARY]
GO
Insert Into [dbo].[marks] Values (3)
Insert Into [dbo].[marks] Values (3)
Insert Into [dbo].[marks] Values (1)
Insert Into [dbo].[marks] Values (4)
Insert Into [dbo].[marks] Values (6)
1--.WITHOUT WITH
select * ,
DENSE_RANK() OVER
( ORDER BY i.[score] DESC) AS Rank
from [dbo].[marks] I
2 -WITH WITH
;WITH CTE1 ([score], ROWID)
AS
-- Define the CTE query.
(
select [score] ,
ROW_NUMBER() OVER (ORDER BY [score] ASC) AS ROWID
from ( select distinct [score] from [dbo].[marks] ) A
)
select [student_id], [marks].[score],DENSE_RANK=ROWID from [marks] left join CTE1
On [marks].[score]=CTE1.[score]
Order By ROWID ------解决方案--------------------