SQL SERVER – Example of DISTINCT in Aggregate Functions

Just a day ago, I was was asked this question in one of the teaching session to my team members. One of the member asked me if I can use DISTINCT in Aggregate Function and does it make any difference.

Of course! It does make difference. DISTINCT can be used to return unique rows from a result set and it can be used to force unique column values within an aggregate function.

USE AdventureWorks
GO
SELECT SUM(DISTINCT ReorderPoint) ResultDistinct
FROM Production.Product
GO
SELECT SUM(ReorderPoint) ResultNoDistinct
FROM Production.Product
GO

SQL SERVER - Example of DISTINCT in Aggregate Functions distinctaggregate

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Function, SQL Scripts
Previous Post
SQL SERVER – Order Of Column In Index
Next Post
SQL SERVER – Create a Comma Delimited List Using SELECT Clause From Table Column

Related Posts

2 Comments. Leave new

  • Its very urgent..

    I have a table called Permissions. in that I have iParentID, iPermissionID there….if I give the iParentId=1, suppose this record having the iPermissionId s 6,7,8. I need to display hirarchy wise…

    can anyone help me regarding this

    Reply

Leave a Reply