Jump to content

Subquery help needed


ViperSBT

Recommended Posts

SELECT r.club, MIN(r.besttime) as time, c.cname, t.date
FROM racing r
JOIN clubs c ON r.club = c.cnumber
INNER JOIN tournament t ON t.enumber = r.event && t.date >= '2007-01-01' && r.besttime < 99.999
GROUP BY r.club
HAVING DATEDIFF( MAX( date ) , MIN( t.date ) ) >4
ORDER BY time, c.cname

 

The above query generates a list of clubs that have participated in more than two events that are greater than 4 days apart over the last 18 months.  However, I need to limit the MIN(r.besttime)  to only look at the last 12 months.  Any thoughts?

Link to comment
Share on other sites

I tried this, which seems to work and is correct, but it takes about 10 minutes to process:

SELECT r.club, MIN(r.besttime) as time, c.cname, t.date
FROM racing r
INNER JOIN clubs c ON r.club = c.cnumber
INNER JOIN tournament t ON t.enumber = r.event && t.date >= '2007-08-01' && r.besttime < 99.999
WHERE r.club IN
(SELECT r.club
FROM racing r
INNER JOIN tournament t ON t.enumber = r.event && t.date >= '2007-01-01' && r.besttime < 99.999
GROUP BY r.club
HAVING DATEDIFF( MAX( date ) , MIN( t.date ) ) >4)
GROUP BY r.club
ORDER BY time, c.cname

Link to comment
Share on other sites

I don't know why I am not "JOIN the derived tables", I am still very green at all this and only know enough to be dangerous.

 

As for the explanation of the query:

 

SELECT r.club, MIN(r.besttime) as time, c.cname, t.date

Here I am looking for the club id, the fastest time that the club has ever run, the club name, and the date of the event that they ran that time.

 

INNER JOIN tournament t ON t.enumber = r.event && t.date >= '2007-08-01' && r.besttime < 99.999

This join is to limit the scope of the club's fastest time to a period of the last 12 months.  So, even though I may have times for them stretching back 3 years, I only want their fastest time during the last 12 months.  In production I am using a variable here instead of a fixed date.

 

WHERE r.club IN
(SELECT r.club
FROM racing r
INNER JOIN tournament t ON t.enumber = r.event && t.date >= '2007-01-01' && r.besttime < 99.999
GROUP BY r.club
HAVING DATEDIFF( MAX( date ) , MIN( t.date ) ) >4)

This part of the query is doing a check against all the entries I have in the racing table to only provide me with those clubs that have entered more than one event during the last 18 months (again in production I am using a variable) and those events had to be more than 4 days apart from each other.

 

So in plain language I am looking for all clubs that have competed in more than two events, greater than 4 days apart, during the last 18 months and need to know the fastest time they ran during the last 12 months.

Link to comment
Share on other sites

OK, I'll buy that JOINs don't work well with ranges, but how else would you do this query?

You write a bunch of individual query, and then then join them together... "derived" tables, as it were.  I don't have time now, maybe later on this evening I will try and work with your schema.

Link to comment
Share on other sites

I'm confused again... sorry, it's been a while.

 

This gets your clubs for 18 months (though I don't see where the 2 event criterion is satisfied):

 

SELECT r.club, DATEDIFF( MAX( date ) , MIN( t.date ) ) AS diff

FROM racing r

INNER JOIN tournament t ON ( t.enumber = r.event )

WHERE t.date >= '2007-01-01' AND r.besttime < 99.999

GROUP BY r.club

HAVING diff > 4

 

Now, the next step you want to do is a) limit this to 12 months and, more importantly, b) just get the min() of some column?

Link to comment
Share on other sites

Yes, you are on track.  The 2 events criterion is satisfied by ensuring that the (diff)erence between event dates is >4.

 

Once we have the list of clubs for 18 months, we need to analyze the best times of that list but only looking at the last 12 months for the times.

Link to comment
Share on other sites

Why not this?

 

SELECT club, MIN(r.besttime) FROM

( SELECT r.club, r.besttime, r.date, DATEDIFF( MAX( r.date ) , MIN( t.date ) ) AS diff

FROM racing r

INNER JOIN tournament t ON ( t.enumber = r.event )

WHERE t.date >= '2007-01-01' AND r.besttime < 99.999

GROUP BY r.club

HAVING diff > 4 ) AS sub

WHERE r.date >= '2007-08-01'

GROUP BY club

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.