过滤postgresql查询结果

时间:2012-04-07 10:05:31

标签: sql postgresql filter

我有多个表需要合并为一个。

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

输出是人名(第一栏)和人的钱(第二栏)。

name | money
aaron  1220
mike   800
john   200
kate   600

现在我尝试按金额过滤结果,即显示“Sum(money)> 500”的人的结果

因为这个原因我试着把“WHERE money> 500 GROUP BY name”但输出错了(aaron 1000,mike 610等)。

如何编写查询来过滤最后/结束结果?

2 个答案:

答案 0 :(得分:4)

Group By Name
having sum(money) > 500

编辑:你对“最后/结局结果”有什么看法?

答案 1 :(得分:4)

如果您希望SUM(money)大于500,那么您可以这样做:

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