Jack Smith
Jack Smith

Reputation: 63

Get sum of column with distinct id in SQL

I am trying to get sum of the amount on distinct ext_id.

Id Ext_id amount
1 234 5
2 234 5
3 235 10
4 236 8
5 236 8

Select SUM(amount) from Table1 -- this will get me sum of all  => 36

I want just sum of distinct Ext_id which should be 23. Can someone help me with this? THanks in advance!

Upvotes: 0

Views: 897

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

You can use two levels of aggregation:

select sum(avg_amount)
from (select ext_id, avg(amount) as avg_amount
      from t
      group by ext_id
     ) x

Upvotes: 1

Related Questions