Alex
Alex

Reputation: 5227

filter postgresql query result

I have multiple tables that need to be merged into one.

SELECT name, SUM(money) AS MONEY FROM transactions
JOIN results ON transactions.id = results.id
JOIN more ON results.per_id = more.per_id
GROUP BY name

The output is person's name (first column) and person's money (second column).

name | money
aaron  1220
mike   800
john   200
kate   600

Now I try to filter the result by a money amount, i.e. to show results for people with "Sum(money) > 500"

for that reason I tried putting "WHERE money > 500 GROUP BY name" however the output was wrong (aaron 1000, mike 610, etc..).

how do you write a query to filter the last/ending result?

Upvotes: 1

Views: 3022

Answers (2)

Arion
Arion

Reputation: 31239

If you want to have the SUM(money) that is greater then 500. Then you can do like this:

SELECT name, SUM(money) AS MONEY FROM transactions
JOIN results ON transactions.id = results.id
JOIN more ON results.per_id = more.per_id
GROUP BY name
HAVING SUM(money)>500

Upvotes: 4

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

Group By Name
having sum(money) > 500

EDIT : what do you meen by "last / ending result" ?

Upvotes: 4

Related Questions