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

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads

2 thoughts on “SQL SERVER – Example of DISTINCT in Aggregate Functions

  1. 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

    Like

  2. Pingback: SQL SERVER – Weekly Series – Memory Lane – #032 | Journey to SQL Authority with Pinal Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s