If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Query help... TOP 5,

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-04, 22:27
blocka blocka is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
Query help... TOP 5,

Hi i am new to this SQL stuff, as before have been able to the simple things in MS Access design view.
But i am having troubles acheiving what i need.
The situation is i have a database for horse racing stats.
There is a few tables, the ones that are related to this are
1. raceList - has raceId, date ... - a list of races
2. raceStarters - raceId, horseId ... - a list of horses in each race
3. previousForm - horseId, date etc...

what i want to be able to do is have a query that for a particular raceId, will display the last 5 previousForm records (sorted by date DESC), for each horseId, in that raceId.

Hope this is clear and someone can help...

Thanks,
Blocka
Reply With Quote
  #2 (permalink)  
Old 04-07-04, 08:54
harshal_in harshal_in is offline
Registered User
 
Join Date: Jan 2003
Location: India
Posts: 523
Re: Query help... TOP 5,

I hope this is what u want:

select top 5 * from previousForm where horseid in (select horseid from racestarters where raceid=@raceid )
order by date desc



replace the @raceid with the actual raceid.


regards,
Harshal.
Reply With Quote
  #3 (permalink)  
Old 04-07-04, 17:57
blocka blocka is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
thanks harshal_in, but it doesn't quite do what i wanted.
The query you have given returns only a total of 5 previousForm results for all horseId's.
What i need is that it will return 5 previousForm for each horseId. So that if there are 8 horseId in a raceId the query will return 40 previousForm results.
Cheers,
Blocka..
Reply With Quote
  #4 (permalink)  
Old 04-07-04, 18:11
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
This is a shot in the dark (I haven't tried it), but:
PHP Code:
SELECT *
   
FROM previousForm AS a
   WHERE  a
.date IN (SELECT TOP 5 b.date
      FROM previousForm 
AS b
      WHERE  b
.horseId a.horseId
         
AND EXISTS (SELECT *
            
FROM raceStarters AS c
            WHERE  c
.horseId b.horseId
               
AND c.raceId = @raceId)
      
ORDER BY b.date DESC
-PatP
Reply With Quote
  #5 (permalink)  
Old 04-07-04, 18:24
blocka blocka is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks Pat that does indeed seem to work from a quick try. Now i just have to try and understand your query.
Thanks again.
Reply With Quote
  #6 (permalink)  
Old 04-07-04, 23:23
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The indentation is for a reason... The further right a line begins, the deeper it is logically nested. Start at the innermost stuff and work your way out and it shouldn't be too bad.

The SELECT aliased as c finds the horses that started in a given race. The SELECT aliased as b finds the most recent 5 dates for a given horse. The SELECT aliased as a is the one that actually produces the rows that go into the result set.

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On