Skip to content
Advertisement

Sum unequal and removing duplicates from SQL query results

My base query:

It can get me these outputs:

Output example:

Obs: There is a constraint on (id, on_date) meaning there can always be only one record of a project production on a specific date.

Duplicate records, that have the same id, and exist in both dates and have different production values (wanted)

Single records, that exists on only one of the dates (wanted)

The problem:*

Duplicate records, that have the same id, and exist in both dates and have equal production values (not wanted)

My current query, that need change

output example:

The CASE is currently not taking care of the third constraint (Duplicate records, that have the same id, and exist in both dates and have EQUAL production values (not wanted))

Is there any possible condition that can accommodate this?

Advertisement

Answer

One option uses not exists to drop rows that have the same id, and exist in both dates and have equal production values:

In MySQL 8.0, you can use window functions:

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