I'm writing a query that will produce the athlete's id, their name (first name and last name combined into one column), the total number of games they have participated in, and the total number of medals they have won. This involves a number of tables.
In one query, the number of games will always become the same as the medals which is incorrect:
SELECT ATHLETE.Athlete_id AS ID,(ATHLETE.Athlete_lname+', '+ATHLETE.Athlete_fname) AS Name, COUNT(CONTESTANT.Rep_id) AS [# of Game], COUNT(MEDAL.Cont_id) AS [# of Medal]
FROM MEDAL, ATHLETE, CONTESTANT, REPRESENTATIVE, GAME
WHERE MEDAL.Cont_id = CONTESTANT.Cont_id
And CONTESTANT.Rep_id=REPRESENTATIVE.Rep_id
And REPRESENTATIVE.Game_id=GAME.Game_id
And REPRESENTATIVE.Athlete_id=ATHLETE.Athlete_id
GROUP BY ATHLETE.Athlete_id, ATHLETE.Athlete_lname, ATHLETE.Athlete_fname
HAVING COUNT(MEDAL.Cont_id)>0
ORDER BY COUNT(MEDAL.Cont_id) DESC;
In a seperate query the number of games works:
SELECT ATHLETE.Athlete_id, COUNT(REPRESENTATIVE.Athlete_id) AS [# of Game]
FROM REPRESENTATIVE, ATHLETE
WHERE ATHLETE.Athlete_id=REPRESENTATIVE.Athlete_id
GROUP BY ATHLETE.Athlete_id;
I also tried a union which produced the correct numbers but the id and name were in the same column, and the medals and games were in the same column one under each other:
SELECT (ATHLETE.Athlete_lname+', '+ATHLETE.Athlete_fname) AS [Name], COUNT(REPRESENTATIVE.Athlete_id) AS [# of Game]
FROM REPRESENTATIVE, ATHLETE
WHERE ATHLETE.Athlete_id = REPRESENTATIVE.Athlete_id
GROUP BY ATHLETE.Athlete_lname, ATHLETE.Athlete_fname
UNION
SELECT ATHLETE.Athlete_id AS [ID], COUNT(MEDAL.Cont_id) AS [# of Medal]
FROM MEDAL, ATHLETE, CONTESTANT, REPRESENTATIVE
WHERE MEDAL.Cont_id = CONTESTANT.Cont_id
AND CONTESTANT.Rep_id = REPRESENTATIVE.Rep_id
AND REPRESENTATIVE.Athlete_id = ATHLETE.Athlete_id
GROUP BY ATHLETE.Athlete_id
HAVING COUNT(MEDAL.Cont_id) > 0;
Any help would be greatly appreciated.