Skip to content
Advertisement

SUM() OVER (PARTITION BY ) AS – When Duplicates Exist

I have an issue with using SUM() OVER(PARTITION BY) where I have duplicate records. I can remove the duplicates using DISTINCT but the SUM still performs over all duplicate records.

For instance my data is:

With DISTINCT applied it becomes:

So the issue is I would hope the SUM column to be 50.

The SUM code is:

I would like to see:

Any advice is appreciated. Thanks.

Advertisement

Answer

You could first select distinct in a subquery, then do the window sum:

This would produce a resultset with 2 records and a total of 50.

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