matshyeq <matshyeq@gmail.com> writes:
> Can I make custom aggregate function that accepts WITHIN GROUP syntax?
It's possible, but it would have to be written in C, I think; see
src/backend/utils/adt/orderedsetaggs.c for prototype code. You'll
find it a lot easier to write a plain aggregate function and control
its input ordering with "last(foo order by foo)".
regards, tom lane