一个表
id int, cid int
有100万条记录,求一个sql语句可以对每个cid取id最大的3条记录。如:
id cid
------
10 1
9 1
8 1
27 2
26 2
25 2
....
id int, cid int
有100万条记录,求一个sql语句可以对每个cid取id最大的3条记录。如:
id cid
------
10 1
9 1
8 1
27 2
26 2
25 2
....
解决方案 »
- "由于发生错误0x08007007e,程序未能注册."怎么回事呀,请都高手.谢谢.
- 关于两个字段排序的问题!!!!!!!!!!!!!!!!!
- win2003,sql server2000的服务管理器设为自动启动,为什么有时候系统启动后自动启动不了
- 求 ASP+SQL SERVER 不用组件实现文本和图片同时上传保存和下载显示的方法
- 请教大家,关于一个联合查询分页的问题
- 像这样的sql语句怎么写!
- 一个小样儿函数的请教,高分赠送,谢谢
- [急!]请教SQL2000对Oracle9i设置链接服务器的问题
- 紧急求助!!! 数据库表被我给删了,没有备份文件,如何还原?
- 求助:怎样将TXT档倒入SQL数据库的表里(原用VF编程, 现改用VB + SQL)?
- 简单SQL问题....下班结贴.速度~~~~~~~~~~~~
- 请问如何创建SQL2000和sql2005的数据源啊
where id in (select top 3 id from 表 where cid=a.cid order by id desc)
from table1 a
where id in (select top 3 id from table1 where cid=a.cid order by id desc)
用ESISTS 要比IN的效率要高,查询速度快
(select 1 from tb group by cid having count(cid)>=3 ) order by id desc)
select * from yourTable A
where (select count(1) from yourTable where cid=A.cid and id>A.id)<3
你的句子确定能得到LZ想要的结果?
id为pk,cid建立了索引,但并非聚集索引,有没有哪位高手可以给出一个快捷的解决方案?
这差不多已经是最快的写法了.
给cid,id加上索引看看.
select top id, cid from a where cid = 1
uinon all
select top id, cid from a where cid = 2
union all
...
的查询比较迅速。
select top id, cid from a where cid = 1
uinon all
select top id, cid from a where cid = 2
union all
...这样是迅速了...CID如果有几千种可能你怎么写?
这个CID字段肯定有索引吧...应该不用遍历表的,索引查询就行了。