user1973799
user1973799

Reputation: 43

Using Distinct and Sum in my sql

below is the table i have created, my goal is to remove the duplicate results then add the value up.

|  username   |   value
|    Bobby    |     5
|    Bobby    |     5
|    Bobby    |     7

This is the result I am looking for

|  username   |   value
|    Bobby    |     12

How can I achieve this?

Upvotes: 0

Views: 81

Answers (4)

ALOK
ALOK

Reputation: 553

Here's simpler way

SELECT distinct(username), sum(distinct value) AS Value from temp1

It works for sure

Upvotes: 0

Nitu Bansal
Nitu Bansal

Reputation: 3856

please try this

 select username,sum(value) from 
 (
   select username,value from tbl group by username,value
 )data
  group by name

Upvotes: 0

naveen goyal
naveen goyal

Reputation: 4629

Try this

select username, sum(value) from 
(select distinct username,value from mytbl) as tmp  group by username

Upvotes: 1

Manish Sapkal
Manish Sapkal

Reputation: 6191

SELECT SUM(DISTINCT value) FROM TableName Group By UserName

Upvotes: 0

Related Questions