Skip to content
Advertisement

Query returns duplicate values [closed]

The query below is showing some duplicate and wrong values:

The output should look like this:

But it’s coming out like this:

Tables schema:

enter image description here

Advertisement

Answer

What happens

The problem is that view_stats and submission_stats have multiple rows per challenge_id.

The JOINs in the query happen before the GROUP BY and the SUM. So imagine, the result set of your query without GROUP BY and SUM.

A simplified example would be:

The result of

would be

Mind the duplicate 1 in the vy column, although, in the original y table there is only one row. This happens because, for id=1 there are two rows in table x. These are joined first, thereby also duplicating the rows of the ids table. Then y is joined to these already duplicated rows which duplicates the rows of y too. When SUM‘ing and grouping, we end up with.

You can find a dbfiddle with the simplified example to play around here.

Solution

There are multiple ways to solve this. The most intuitive is to GROUP and SUM the rows of view_stats and submission_stats before joining them.

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