The database is quite simple. Below there is a part of a schema relevant to this question
-
ROUND (
round_id, round_number) -
TEAM (
team_id, team_name) -
MATCH (
match_id, match_date, round_id) -
OUTCOME (
team_id, match_id, score)
I have a problem with query to retrieve data for all matches played. The simple query below gives of course two rows for every match played.
select *
from round r
inner join match m on m.round_id = r.round_id
inner join outcome o on o.match_id = m.match_id
inner join team t on t.team_id = o.team_id
How should I write a query to have the match data in one row?
Or maybe should I redesign the database – drop the OUTCOME table and modify the MATCH table to look like this:
- MATCH (
match_id, match_date, team_away, team_home, score_away, score_home)?
You can almost generate the suggested change from the original tables using a self join on
outcometable:Of course, the information for home and away are not possible to generate, so your suggested alternative approach might be better after all. Also, take note of the condition
o1.team_id < o2.team_id, which gets rid of the redundant symmetric match data (actually it gets rid of the sameoutcomerow being joined with itself as well, which can be seen as the more important aspect).In any case, using this select as part of your join, you can generate one row per match.