create table A

(

Id int identity,

Name char(10)

)

create table  B

(

Id int,

[SubJect] char(10),

Score int

)

--1.查询各科成绩排名前二名的人的编号 No 姓名Name 科目Subject 分数Score 

select * from (SELECT *,rn=ROW_NUMBER() OVER(PARTITION BY subject ORDER BY  score desc)

FROM B) t where t.rn<=2



限制结果集。返回第3行到第5行的数据

( ROW_NUMBER 不能用在where子句中,所以将带行号的执行结果作为子查询,就可以将结果当成表一样用了):

SELECT * FROM

 

SELECT ROW_NUMBER() OVER(ORDER BY FSalary DESC) AS rownum, 

FNumber,FName,FSalary,FAge FROM T_Employee 

AS a

 

WHERE a.rownum>=3 AND a.rownum<=5