返回信息流题目如下:
表名:testshow
ID 名称 种类ID 显示次数
ID fname sortID showtimes
1 A 1 200
2 B 1 340
3 C 2 1000
4 D 1 130
5 E 3 360
6 F 2 400
要求:根据种类ID(sorid)查询出2个显示次数(showtimes)最高的所有信息。
万分感谢!
这是一条镜像帖。来源:北邮人论坛 / database / #2582同步于 2008/10/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖
[求助]大唐微电子一道数据库题
wuzw091
2008/10/19镜像同步15 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
select top 2 * from (select * from table order by showtimes)
sqlserver 下应该可以
select * from (
select row_number() over (partiton by sortID order by showtimes desc) rank,
sortID,showtimes
from testshow
)
where rank in (1,2)
我只想到了一种 showtimes 不重复时的方法(至少每个sortID内)。
select sortID,max(showtimes) as s from testshow group by sortID
union
select sortID,max(showtimes) as s from (select * from testshow where (sortID
,showtimes) not in (select sortID,max(showtimes) as s from testshow group by
sortID))
这样就联出了一个每种的前两个最大次数的表 然后在原表中继续找 对应信息就够了。
。。
select * from testshow where (sortID,showtimes) in
(
select sortID,s from
(select sortID,max(showtimes) as s from testshow group by sortID
union
select sortID,max(showtimes) as s from
(select * from testshow where (sortID,showtimes) not in (select sortID,ma
x(showtimes) as s from testshow group by sortID)
)
)
)
order by sortID,showtimes;
【 在 wuzw091 (随风而飘) 的大作中提到: 】
: 题目如下:
: 表名:testshow
: ID 名称 种类ID 显示次数
: ...................
太强悍了。
【 在 kdsyan 的大作中提到: 】
: select * from (
: select row_number() over (partiton by sortID order by showtimes desc) rank,
: sortID,showtimes
: ...................