SQL Server Eliminate duplicate entries from my query results when using window function

sy5wg1nm  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(90)

Given following query:

select *
from (
    select name, vote_date
        , sum(votes) over (partition by name) as total_votes
    from table
) as subq
where vote_date > '2023-01-01'
and total_votes > 4

This is a sample output:

vote_datenametotal_votes
2023-01-08Alex6
2023-07-14Bob8
2023-07-19Bob8
2023-04-18Carol9
2023-02-03Carol9
2023-07-20Diana10
2023-05-22Diana10
2023-08-21Rizlf10
2023-09-27Rizlf10

I want to eliminate duplicates form my results and only show one entry for each name with the total_votes. The data has the same person voting across multiple days

I have tried to use a GROUP BY name but I get an error. Column 'subq.vote_date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I have also tried removing the vote_date from the subquery but I get another error I think because I need it for the WHERE clause.

20jt8wwn

20jt8wwn1#

Not sure why vote_date is in the output. Maybe you just want:

SELECT name, SUM(votes) as total_votes
FROM dbo.tableName
WHERE vote_date > '20230101'
GROUP BY name;

Or if you want the last date per person:

SELECT name, 
  MAX(vote_date) as last_vote,
  SUM(votes) as total_votes
FROM dbo.tableName
WHERE vote_date > '20230101'
GROUP BY name;

But having all the vote dates in the output and also trying to group by it or eliminate duplicates that are only different in the column can’t all be done at the same time.

相关问题