Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Distinct option with SUM function

Author  Topic 

archanasql
Starting Member

27 Posts

Posted - 2007-12-14 : 16:06:21
Hi,

I have a query with DISTINCT option. If i use DISTINCT option with aggregate function like SUM will it eliminate duplicate values and then sum the values of a column and what is the syntax for it.

EX I have a column for budget

BUDGET
100
350
275
350
100

so if i use DISTINCT SUM(BUDGET) will it eliminate duplicate values and sum the total.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-12-14 : 16:25:44
No, maybe an example would help:
DECLARE @Budget TABLE (Budget MONEY)

INSERT @Budget
SELECT 100
UNION ALL SELECT 350
UNION ALL SELECT 275
UNION ALL SELECT 350
UNION ALL SELECT 100

SELECT DISTINCT SUM(Budget)
FROM @Budget

SELECT SUM(DISTINCT Budget)
FROM @Budget
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-14 : 16:26:56
Be sure to read this:

http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables

(maybe it would help to read part I first)

If you have duplicates in your data, fix that FIRST, and THEN sum up your data, as the article(s) demonstrate.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

archanasql
Starting Member

27 Posts

Posted - 2007-12-14 : 16:51:53
Thanks a lot. It worked out!!!
Go to Top of Page
   

- Advertisement -