我把表缩写成stu(sid varchar(8), cid varchar(10), score int), 这样可以不?要不要考虑并列情况我的意思是:如果A在数学拿了最高分90, B和C拿了次高分89。这样的话, 如何输出?
A, B, C还是A, B, 还是A, C?
A, B, C还是A, B, 还是A, C?
解决方案 »
- sql 统计
- 如何产生指定范围内的浮点数和整数,谢谢!
- 当数据库里的数据大于3万条时,是否采用存储过程比直接使用sql代码会快很多呢?
- PB+sql2000调用存储过程,取取服务器时间的问题啊
- sqlserver执行webservice中文参数乱码
- 执行一个存储过程,要求声明变量的问题
- [原]SQLSERVER 常用KPI的计算.
- Execl里数据导到多个数据库里,有没有好的方法
- 跨服务器使用存储过程插入数据的使用使用到了事务!
- SQL Server 与 Access 有什么不同,怎样从 ACCESS 开始学习 SQL SERVER,谢谢!
- SQL查找为什么这么变态
- 菜鸟提问,高手请答!--一个简单的查询:
create table stu(sid varchar(8), cid varchar(10), score int)
insert stu select '001', '001', 90
union all select '002', '001', 89
union all select '003', '001', 89
union all select '004', '001', 80
union all select '001', '002', 92
union all select '002', '002', 93
union all select '003', '002', 88
--查询(考虑并列的情况)
select sid, cid, score
from (
select *
,id=(select count(*)+1 from stu
where cid=tt.cid and score>tt.score)
from stu as tt)t
where t.id<=2
order by cid, score desc
--清除
drop table stu
create table stu(sid varchar(8), cid varchar(10), score int)
insert stu select '001', '001', 90
union all select '002', '001', 89
union all select '003', '001', 89
union all select '004', '001', 80
union all select '001', '002', 92
union all select '002', '002', 93
union all select '003', '002', 88select * from stu order by cid,scoreselect A.* from stu A where not exists(select 1 from stu where cid=A.cid and score>A.score having count(*)>1) drop table stu
FROM stu a
WHERE ((SELECT COUNT(*)
FROM stu b
WHERE a.cid = b.cid AND a.score < b.score) < 2)
ORDER BY score DESC