0

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!

Jack Smith
  • 63
  • 1
  • 7
  • Does this answer your question? [Trying to sum distinct values SQL](https://stackoverflow.com/questions/18218108/trying-to-sum-distinct-values-sql) – VladNeacsu Apr 09 '21 at 05:25

1 Answers1

1

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
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786