Hi there,
I want to select some entries based on a max+sum condition.
mytable
----------
id | col1 | col2
I want to select all entries that have the sum of col1 & col2 greater than or equal to the max of sum minus X. (don't ask me why :) )
So far I managed to get the sum OK (hereafter aliased as "total") with:
SELECT id,SUM(col1 + col2) AS total FROM mytable GROUP BY id;
I also managed to get the MAX of the sum OK (with a ORDER BY/LIMIT workaround though):
SELECT id,SUM(col + col) as total FROM mytable GROUP BY id ORDER BY total DESC LIMIT 1;
However everytime I try to re-use my alias as a condition (e.g. WHERE total >= ...) I get an "Unknown column" error
Anything would be greatly appreciated