So, let's say I have a hash/relational table that connects users, teams a user can join, and challenges in which teams participate (teams_users_challenges
), as well as a table that stores entered data for all users in a given challenge (entry_data
). I want to get the average scores for each user in the challenge (the average value per day in a given week). However, there is a chance that a user will somehow join more than one team erroneously (which shouldn't happen, but does on occasion). Here is the SQL query below that gets a particular user's score:
SELECT tuc.user_id, SUM(ed.data_value) / 7 as valueFROM teams_users_challenges tucLEFT JOIN entry_data ed ON ( tuc.user_id = ed.user_id AND ed.entry_date BETWEEN '2013-09-16' AND '2013-09-22')WHERE tuc.challenge_id = ___AND tuc.user_id = ___
If a user has mistakenly joined more than one team, (s)he would have more than one entry in teams_users_challenges
, which would essentially duplicate the data retrieved. So if a user is on 3 different teams for the same challenge, (s)he would have 3 entries in teams_users_challenges
, which would multiply their average value
by 3, thanks to the LEFT JOIN
that automatically takes in all records, and not just one.
I've tried using GROUP BY
, but that doesn't seem to restrict the data to only one instances within teams_users_challenges
. Does anybody have any ideas as to how I could restrict the query to only take in one record within teams_users_challenges
?
ADDENDUM: The columns within teams_users_challenges
are team_id
, user_id
, and challenge_id
.