Does DISTINCT in a simple query with an aggregate function have any effect?
select DISTINCT salesperson, SUM(sales_amt) from sales GROUP BY salesperson
I realize there are more complicated queries where DISTINCT can have an affect, such as:
select salesperson, SUM(DISTINCT sales_amt) from sales GROUP BY salesperson
(support for that syntax by platform may vary)
But I want to confirm that in the simple query example, DISTINCT is redundant.
EDIT: fixed missing GROUP BY salesperson
Assuming you are missing
GROUP BY salesperson(it’s invalid in SQL Server if you omit the group by), theDISTINCTis redundant in your first query. TheGROUP BYeffectively performs aDISTINCThere by aggregatingsalesperson.And you are, as you’ve noted, correct that placement of the
DISTINCTinside the aggregateSUM()may produce a different rowset.