50分求一SQL查询语句(+100)

来源:百度知道 编辑:UC知道 时间:2024/05/09 18:43:20
表 info(大约有300W条记录)
字段: id(自增1,主键) ,content(内容),types(整型) ,addtime(时间类型)
说明:
1:info(大约有300W条记录);
2:同一时间的段的记录有1到N条;
3:排序规则,按addtime逆序,按types逆序,同一时间段的信息只查询出5条记录(多不要,少不补)
4:一次查询出40条记录,实现分页功能。
请帮忙写下,正确后再加100分!
会的帮忙写下,不会的一起研究下。不知道我说话描述的是不是有问题,被误解了。给分只是我表示感激的一种方式。别无它意!
问题还没有完全解决.
先做了一个视图,存放所有相同的时间。
然后在存储过程中通过游标,重新做一个临时表,访问临时表就可以了,不过这个方法效率实在很差

参考:
--创建测试数据
declare @t table(id int,content varchar(20),addtime int,types varchar(50))
insert @t
select 1,'张一',100,'财务部' union all
select 2,'张二',100,'财务部' union all
select 3,'张三',200,'财务部' union all
select 4,'张四',300,'财务部' union all
select 5,'张五',100,'生产部' union all
select 6,'张六',200,'生产部' union all
select 7,'张七',200,'生产部' union all
select 8,'张八',300,'生产部' union all
select 9,'张九',100,'行政部' union all
select 10,'张十',100,'行政部' union all
select 11,'张十一',200,'行政部' union all
select 12,'张十二',200,'行政部'

select * from @t x
where addtime in
(select top 3 addtime from @t y where y.types = x.types order by addtime desc)
order by types,addtime desc

--结果
/*
id content addtime