如何执行GROUP BY SUM()

时间:2012-07-16 12:23:04

标签: sql group-by

SELECT x,y,SUM(f)-SUM(g) 
FROM a,b 
WHERE a.x = b.x 
GROUP BY SUM(f)-SUM(g)
HAVING SUM(f)-SUM(g)>1

尝试使用GROUP BY x时,它可以正常工作,但我怎样才能进行GROUP BY SUM(f)-SUM(g)??

1 个答案:

答案 0 :(得分:4)

您不需要按聚合列进行分组。你需要的是

SELECT x,y,SUM(f)-SUM(g)  
FROM a,b  
WHERE a.x = b.x  
GROUP BY x,y 
HAVING SUM(f)-SUM(g)>1