mysql - Using SUM to find total amount in columns and then group by person -


i have list of amounts in transactions table. want find out total transaction amount each person_id has total amount greater 50.

i hoping work didn't:

select (     select sum(amount)      transactions person_id = p.id ) total_amount people p  total_amount > 50 

the way work following:

select (     select sum(amount)      transactions person_id = p.id ) total_amount people p  (     select sum(amount)      transactions person_id = p.id ) > 50 

.. super inefficient. suggestions on how can format query better?

try

select person_id, sum(amount)   transactions group person_id having sum(amount) > 50 

sqlfiddle

update: people , transactions joined

select t.person_id, p.name, sum(t.amount) amount   transactions t join        people p on t.person_id = p.id  group t.person_id, p.name having sum(t.amount) > 50 

sqlfiddle


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -