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. The quick answer is yes, and a small example of DISTINCT in Aggregate Functions shows why.

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

SELECT DISTINCT ResultDistinct.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Function, SQL Scripts
Previous Post
SQL SERVER – SQL SERVER – UDF – Get the Day of the Week Function Part 4
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

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.