FILTER clause for non-aggregate window functions

From: Андрей Жиденков <pensnarik(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: FILTER clause for non-aggregate window functions
Date: 2017-12-12 07:54:14
Message-ID: CAN=gQ4AG_XxuTSq2brj3GY_MK-Qc+KwnETQiMXGmycsgy_Mbhw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I need to find the first date value in widow which meets certain
conditions. In my case I use min() function with CASE like this:

SELECT min(CASE WHEN <conditions> THEN <date_field> end) OVER (PARTITION BY
.. ORDER BY <date_field> ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)

But this is too slow mainly because of min() should read all tuples in
window, I guess. So I tried to use first_value() function like this:

SELECT first_value(<date_field>) FILTER(WHERE <conditions>) OVER (PARTITION
BY .. ORDER BY <date_field> ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED
FOLLOWING)

But I got this error:

FILTER is not implemented for non-aggregate window functions

Why FILTER is not implemented for non-aggregate functions? Is there some
restrictions in PostgreSQL executor core or maybe this behavior will lead
to some conflicts? Is there a way to find needed value without scanning all
tuples in window? Any help would be greatly appreciated.

--
Andrey Zhidenkov

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2017-12-12 15:35:05 Re: FILTER clause for non-aggregate window functions
Previous Message Andreas Kretschmer 2017-12-11 18:46:50 Re: Windowing ?