你的查询不正确,应该使用这样的查询,字段名称是班级,年级,分数的拼音,把查询中的5改成10就可以得到前10名了。(SQL Server 2005测试通过)- DROP TABLE STU
- CREATE TABLE STU
- (NIANJI CHAR(5),
- BANJI CHAR(5),
- NAME CHAR(5),
- FENSHU INT)
- INSERT INTO STU VALUES('1','A','A','10')
- INSERT INTO STU VALUES('1','A','B','20')
- INSERT INTO STU VALUES('1','A','C','30')
- INSERT INTO STU VALUES('1','A','D','40')
- INSERT INTO STU VALUES('1','A','E','50')
- INSERT INTO STU VALUES('1','A','F','60')
- INSERT INTO STU VALUES('1','B','G','10')
- INSERT INTO STU VALUES('1','B','H','20')
- INSERT INTO STU VALUES('1','B','I','30')
- INSERT INTO STU VALUES('1','B','J','40')
- INSERT INTO STU VALUES('1','B','K','50')
- INSERT INTO STU VALUES('1','B','L','60')
- INSERT INTO STU VALUES('2','A','M','10')
- INSERT INTO STU VALUES('2','A','N','20')
- INSERT INTO STU VALUES('2','A','O','30')
- INSERT INTO STU VALUES('2','A','P','40')
- INSERT INTO STU VALUES('2','A','Q','50')
- INSERT INTO STU VALUES('2','A','R','60')
- INSERT INTO STU VALUES('2','B','S','10')
- INSERT INTO STU VALUES('2','B','T','20')
- INSERT INTO STU VALUES('2','B','U','30')
- INSERT INTO STU VALUES('2','B','V','40')
- INSERT INTO STU VALUES('2','B','W','50')
- INSERT INTO STU VALUES('2','B','X','60')
- SELECT NIANJI, BANJI, FENSHU
- FROM stu T
- WHERE 5>=
- (SELECT COUNT(*)
- FROM stu TB
- WHERE TB.BANJI=T.BANJI
- AND T.NIANJI=TB.NIANJI
- AND TB.FENSHU>=T.FENSHU
- GROUP BY NIANJI,BANJI)
复制代码 |