obaid
obaid

Reputation: 902

get sum of column if ID is repeated

I have a tables and I want the sum of the column if the ID is repeated

CountValues      ID
____________________
1                23
4                23
2                12
2                23

If ID is repeated then CountValues must be sum of ID itself like below

CountValues      ID
____________________
7                23
2                12

What should be the sql query?

Upvotes: 0

Views: 350

Answers (3)

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7171

Try this query

select sum(CountValues) as CountValues, ID
from TABLE
group by ID

Upvotes: 1

potashin
potashin

Reputation: 44581

You can use sum aggregate function together with the group by clause:

select sum(CountValues) as CountValues, ID
from tbl 
group by ID

Upvotes: 1

Ullas
Ullas

Reputation: 11556

Use the aggregate function SUM with GROUP BY.

Query

select SUM(CountValues) as CountValues, ID
from your_table_name
group by ID;

Upvotes: 0

Related Questions