Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sumup distinct values

Status
Not open for further replies.

habneh

Programmer
Mar 21, 2007
55
US
I want to sum up distinct values of a column how can I do that

ex

col1 col2
1 1
1 1
1 1
1 2
1 2
1 3

the result set should be

col1 col2
1 6

thanks
 
do you mean
col1 col2
1 1
1 1
1 1
1 2
1 2
1 3

the result set should be

col1 col2
1 63



____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done
 
try this:
Code:
select col1, sum(col2) col2
(select distinct col1, col2 from tableName) a
group by col1

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
derr..thinking count()

____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done
 
This one works well (in 2005)
Code:
select 
col1,
sum(distinct col2)  
from @tbl
group by col1

pretty much the same as kahts though

____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done
 
Nah, yours is 1 select statement, mine's 2 [smile]

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top