Forum Moderators: open

Message Too Old, No Replies

Integrating 2 SQL queries

         

mattli

6:09 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



I have a database of cycling races. There is a competion called the Best All Rounder (BAR) which is won by the rider with the greatest average speed over certain events. The following query retrieves the riders that have ridden in all the qualifying events:

select rider_key from rider as Ri where not exists
(select * from course as C where C.club_bar = 1 and not exists
(select * from result as R, event as E
where C.course_key = E.course
and R.event = E.event_key
and R.rider = Ri.rider_key))

and the following the average speed of the best rides on qualifying courses for a given rider (in this case rider 2):

select avg(M) from (select course_key,
(select max(av_speed) from event, result where rider = 2 and event.course = course_key and result.event=event_key and event.incinpb = 1) as M
from course where club_bar = 1) as N

What I would like to be able to do is marry the 2 together so that the final result is a list of riders qualifying (as of 1st query), with their average speed (as of 2nd query) - ordered by average speed, but I can't get my head around how to do it.

Any help would be much appreciated

syber

12:26 am on Feb 16, 2008 (gmt 0)

10+ Year Member



Something like this should work:


select rider_key, (select max(av_speed)
from event, result
where rider = rider.rider
and event.course = course_key
and result.event=event_key and event.incinpb = 1)
from rider as Ri
where not exists
(select * from course as C where C.club_bar = 1 and not exists
(select * from result as R, event as E
where C.course_key = E.course
and R.event = E.event_key
and R.rider = Ri.rider_key))

The trick is to correlate rider in the outer query to rider in the embedded query.

mattli

10:00 am on Feb 16, 2008 (gmt 0)

10+ Year Member



Thanks for that syber, I will have play around and see if I can get it to work.

syber

1:34 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



Oops, the third line of the code should say:

where rider = Ri.rider

mattli

3:33 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



Thanks syber, based on that, I have tried this:

select rider_key, (select avg(M) from (select course_key,
(select max(av_speed) from event, result where rider = Ri.rider_key and event.course = course_key and result.event=event_key and event.incinpb = 1) as M
from course where club_bar = 1) as N)
from rider as Ri
where not exists
(select * from course as C where C.club_bar = 1 and not exists
(select * from result as R, event as E
where C.course_key = E.course
and R.event = E.event_key
and R.rider = Ri.rider_key))

but it doesn't work, coming up with the error

#1054 - Unknown column 'Ri.rider_key' in 'where clause'

syber

3:54 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



Do you know which Where clause caused the error, line 2 or the last line?

mattli

4:03 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



It was line 2 syber

syber

4:40 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



Looks ok to me, are you able to use the JOIN sytax?


(select avg(M)
from (select course_key, (select max(av_speed)
from event JOIN result ON result.event=event.event_key
where result.rider = Ri.rider_key
and event.course = course_key
and event.incinpb = 1) as M
from course
where club_bar = 1) as N)

mattli

4:58 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



Thanks for your efforts, but it still doesn't like Ri.rider_key

syber

5:26 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



The only thing I can think of is to check for a typo. If it likes Ri.rider_key in the last line, it should like it in line 2.

mattli

9:36 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



I can't see any typos, if I substitute
rider = Ri.rider_key

with

rider = 2

then it works (but obviously all rows show rider 2's average)
Is this a question of scope? The query containing rider = Ri.rider_key cannot 'see' the rider table of the second query?

syber

10:26 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



No, it's not a problem with scope. That is how correlated sub-queries are supposed to work.