Re: nth_value and row_number in a partition

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Olivier Leprêtre <o(dot)lepretre(at)gmail(dot)com>
Cc: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: nth_value and row_number in a partition
Date: 2018-01-25 19:57:36
Message-ID: CAKFQuwbB-74cLV1eDZFeqSEjVzAR5QKiUKAnJrYRCoLtM2cDow@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Jan 25, 2018 at 12:49 PM, Olivier Leprêtre <o(dot)lepretre(at)gmail(dot)com>
wrote:

> select roads,orders,(first_value(v1)
>
> over (partition by roads,segment order by orders)-nth_value(v2,row_number()
> over (partition by roads)::integer) over (partition by roads,segment order
> by orders)) as result
>
> from mytable
>
>
>
> >> window call cannot be imbricated
>

​I take it "imbricated" is the word for "nested" in a different language...

​WITH vals AS ( VALUES (1), (2), (3) )
SELECT max( row_number() OVER () ) OVER ()
FROM vals;

Results in:

SQL Error: ERROR: window function calls cannot be nested
LINE 2: SELECT max( row_number() OVER () ) OVER ()

Which means that what you are trying to do (or at least they way you are
trying to do it) cannot be done.

You will need to resort to a subquery to compute the row_number for each
row and then feed that into the upper query as column reference.

David J.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Martin Stöcker 2018-01-25 20:12:39 Re: nth_value and row_number in a partition
Previous Message Olivier Leprêtre 2018-01-25 19:49:25 RE: nth_value and row_number in a partition