Was asked the question in regards to a olympic database.
“For each competitor who was in more than one event, list their given-name, family-name, their number of events, and their best and worst places (and no other data).
List this information in in descending order of their number of events (i.e. most events first),
and (when competitors have the same number of events) then in ascending order of best place,
and (when the same best place) then in ascending order of worst place,
and (when the same worst place) then in ascending order of their family-name.”
I have been trying to write the query to do this, however it does not return the correct count. It stops at a count of 2 and does not return the correct min or max values.
The Tables are:
Competitors Table
Competitornum | Givenname | Familyname | Gender | Dateofbirth | CountryCode |
Results Table has
Eventid | Competitornum | Place | Lane | Elapsedtime | Note |
Query
SELECT c.Givenname, Familyname, COUNT(r.Competitornum), MIN(r.Place), MAX(r.Place)
FROM Competitors c
JOIN Results r
ON c.Competitornum=r.Competitornum
Group by c.Givenname, familyname, place
Having Count(r.Competitornum) > 1
This is the query I have come up with so far and am not sure what I have wrong.
You don’t want to include
placein your GROUP BY clause.And you will need an ORDER BY clause to meet all the requirements.