huz_akh
huz_akh

Reputation: 93

MYSQL - Sum of distinct rows

I am new to MYSQL and need a simple query but can't get it to work.

I have a table

id   amount
 1      30
 2      20
 3      30
 3      30
 4      50

I want the sum of the amount for distinct ids

So the output should simply be 130. (30+20+30+50)

How can I achieve this ?

Thanks.

Upvotes: 1

Views: 1253

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You can use distinct

select sum(amount) from (
select distinct `id`, `amount` from t
  ) t1

Demo

or pick the max amount for same ids

select sum(amount) from (
select `id`, max(`amount`) amount from t group by id
  ) t1

Demo

Upvotes: 1

Related Questions