Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3911177
  • 博文数量: 421
  • 博客积分: 685
  • 博客等级: 上将
  • 技术积分: 3670
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-18 14:20
文章分类

全部博文(421)

文章存档

2012年(5)

2011年(52)

2010年(83)

2009年(67)

2008年(65)

2007年(149)

分类: Oracle

2009-07-15 17:27:57

表table
字段 title,hits,boardid

取每boardid个hits前两位的title值

oracle写法

select title,dense_rank() over(partition by boardid order by hits desc) rn
from topic t
where rn<3



access写法,只能取第一位的值

select b.title,b.hits,b.boardid
from
(select max(hits) as hit ,boardid from dv_topic group by boardid)
as t
left join dv_topic as b on b.hits=t.hit and b.boardid=t.boardid


sql server写法,取每类前两位,access可依样画葫芦,把text改成别的即可

select tt.boardid,tt.title,tt.hits as fistHit,(select top 1 hits from text where text.boardid=tt.boardid and text.hits<tt.hits order by hits desc) as secondHit,
(select top 1 title from text where text.boardid=tt.boardid and text.hits<tt.hits order by hits desc) as secondtitle from
(
select text.title,text.hits,text.boardid
from
(
select max(hits) as hits ,boardid from text group by boardid
)
as t
left join text on text.hits=t.hits and text.boardid=t.boardid
)
as tt


相关阅读

阅读(6878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~