I want to do this, to get all the rows for a given a,b combo that have the highest value of c:
SELECT a, b, c
FROM x
WHERE RANK() OVER (PARTITION BY a,b ORDER BY c DESC) = 1
It fails, saying invalid column reference 'c': (possible column names are: a, b, c).
I can wrap the RANK() in a subselect, and check the value outside:
SELECT a, b, c
FROM (
SELECT a, b, c, RANK() OVER (PARTITION BY a,b ORDER BY c DESC) AS r
FROM x
) rq
WHERE r = 1
Any idea why I can't do this in the WHERE clause of the simple query? Is it because of it being an aggregate/window function, so has to be done after the WHERE, like a GROUP BY?
SELECT a, b, MAX(c) FROM x GROUP BY a,b? – Akina Dec 06 '18 at 12:35