Jesper Krogh <jesper(at)krogh(dot)cc> writes:
> I have been puzzled about the evaluation order when using window
> functions and limit.
It's basically FROM -> WHERE -> window functions -> LIMIT.
> I expected it to either count to 3 or blow up and tell me that count(*)
> wasn't a window function.
Any aggregate function can be used as a window function. It just
aggregates over the whole partition (which in this case is all 8
rows that satisfy the WHERE).
LIMIT is supposed to truncate the output at a specified number of
rows, not change what is in those rows; so ISTM that this evaluation
order is the expected one. If you want to put a LIMIT in front of
the aggregates/window functions, put it in a sub-select.
regards, tom lane