Skip to content
Advertisement

SQL Join tables with no unique values

I am trying to create a table for a game that shows some player and team stats so I can compare them. The two tables look like this:

The thing to note here is that each player has a team, and there are two rows for each distinct game id (since there is one player on each team for each game).

The other table I have is for each team and their aggregate results, so in the above table, the kills/assists/deaths belong to the individual player, while below is the aggregate of the 5 players on each team.

I’m trying to join these columns so I can get a percentage for each player in each game. The problem is that when I try to join on the games column, it creates 4 rows. Player A on team A, Player A on team B, player B on team B, and Player B on team A.

The syntax I’m using is this:

The output:

What you can see here in the first 4 rows is what I described above. I understand that this is a result of the way the join works, but I’m just not sure how to prevent it from doing that. There isn’t really a primary key or other unique single column value I can use to join the tables.

I can create a table that shows each player and what team they were on for each patch using select distinct, and I’m wondering if I can do a second join on that table which would eliminate the erroneous entries, but I figure if there was a way to do that in the first join then that would be more efficient and therefore good to know.

Thanks.

Advertisement

Answer

No special need for primary/foreign keys here. There is missing second condition in the join.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement