Skip to content
Advertisement

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!

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement