2011-05-21 49 views
2

我有我的一些SQL的后数据库表(这是打败了我这么远!)检索顶部从数据库48唯一的记录

试想有192个体育俱乐部谁所有参加每赛季12场比赛。

所以这是2304个个人表演每赛季(例如在100Metres)

我想找到顶级的48(唯一的)个人从表中的表演,这48名运动员,然后去参加本赛季世锦赛结束。所以想象2次最快的时间都是由“约翰史密斯”设定的,但他只能在世界冠军中进入一次。所以我会寻找下一个没有被“约翰史密斯”设置的最快时间...等等,直到我有48个独特的运动员。

希望是有道理的。

在此先感谢如果有人能帮助

PS 我确实有创造,将解释好得多一个不错的屏幕截图。但作为一个新用户,我无法发布图片。

我会尝试复制和粘贴的版本,而不是...

ID AthleteName AthleteID Time 
1 Josh Lewis  3 11.99 
2 Joe Dundee  4 11.31 
3 Mark Danes  5 13.44 
4 Josh Lewis  3 13.12 
5 John Smith  1 11.12 
6 John Smith  1 12.18 
7 John Smith  1 11.22 
8 Adam Bennett 6 11.33 
9 Ronny Bower  7 12.88 
10 John Smith  1 13.49 
11 Adam Bennett 6 12.55 
12 Mark Danes  5 12.12 
13 Carl Tompkins 2 13.11 
14 Joe Dundee  4 11.28 
15 Ronny Bower  7 12.14 
16 Carl Tompkin 2 11.88 
17 Nigel Downs  8 14.14 
18 Nigel Downs  8 12.19 

前4个独特的个人表演

1 John Smith  1 11.12 
3 Joe Dundee  4 11.28 
5 Adam Bennett 6 11.33 
6 Carl Tompkins 2 11.88 
+0

如果您想要特定的SQL查询,我们需要知道您使用的数据库服务器。 – Tony 2011-05-21 23:19:25

回答

2

基本上是这样的:

select top 48 * 
    from (
     select athleteId,min(time) as bestTime 
      from theRaces 
      where raceId = '123' -- e.g., 123=100 meters 
      group by athleteId 
     ) x 
order by bestTime 
+0

谢谢肯。我会尝试并适应该代码的工作。 – user585085 2011-05-21 23:23:09

+0

太棒了!谢谢你的回复。 – user585085 2011-05-21 23:37:20

+0

很好地完成了Ken。简单而有效。 – IAmTimCorey 2011-05-21 23:38:09

0

试试这个 -

select x.ID, x.AthleteName , x.AthleteID , x.Time 
(
select rownum tr_count,v.AthleteID AthleteID, v.AthleteName AthleteName, v.Time Time,v.id id 
from 
(
    select 
    tr1.AthleteName AthleteName, tr1.Time time,min(tr1.id) id, tr1.AthleteID AthleteID 
    from theRaces tr1 
    where time = 
      (select min(time) from theRaces tr2 where tr2.athleteId = tr1.athleteId) 
    group by tr1.AthleteName, tr1.AthleteID, tr1.Time 
    having tr1.Time = (select min(tr2.time) from theRaces tr2 where tr1.AthleteID =tr2.AthleteID) 
    order by tr1.time 
) v 
) x 
where x.tr_count < 48