SUM(DISTINCT) Based on Other Columns
select sum (rate)
from yourTable
group by first_name, last_name
Edit
If you want to get all sum of those little "sums
", you will get a sum of all table..
Select sum(rate) from YourTable
but, if for some reason are differents (if you use a where
, for example)
and you need a sum for that select above, just do.
select sum(SumGrouped) from
( select sum (rate) as 'SumGrouped'
from yourTable
group by first_name, last_name) T1
David said he found his answer as such:
SELECT SUM(rate) FROM (SELECT * FROM records GROUP BY last_name, first_name) T1
But when you do the GROUP BY
in the inner query, I think you have to use aggregate functions in your SELECT
. So, I think the answer is more like:
SELECT SUM(rate) FROM (SELECT MAX(rate) AS rate FROM records GROUP BY last_name, first_name) T1
I picked MAX()
to pick only one "rate" for a "last_name, first_name" combination but MIN()
should work the same, assuming that the "last_name, first_name" always leads us to the same "rate" even when it happens multiple times in the table. This seems to be David's original assumption - that for a unique name we want to grab the rate only once because we know it will be the same.
SELECT SUM(rate)
FROM [TABLE]
GROUP BY first_name, last_name;