agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Andreas Kretschmer <andreas@a-kretschmer.de>
To: pgsql-sql@lists.postgresql.org
Subject: Re: Windowing ?
Date: Mon, 11 Dec 2017 18:15:56 +0100
Message-ID: <f02993c7-c740-e99b-e0e2-010a36ded92d@a-kretschmer.de> (raw)
In-Reply-To: <5a2ebbc9.8a1f1c0a.ea0ea.7b4a@mx.google.com>
References: <5a2ebbc9.8a1f1c0a.ea0ea.7b4a@mx.google.com>



Am 11.12.2017 um 18:09 schrieb Olivier LeprĂȘtre:
>
> Hi,
>
> I have a table describing sort of ordered road segments and their 
> associated weight. I would like to calculate weight deltas between two 
> consecutive segments. Problem is to substract previous from current 
> row and reuses result as input for the next row.
>

You can use the lag() - function.

select ..., lag(weight) over (partition by road order by segment) ...



Regards, Andreas

-- 
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com





view thread (3+ messages)  latest in thread

Message-ID: <f02993c7-c740-e99b-e0e2-010a36ded92d@a-kretschmer.de>
Permalink:  ../f02993c7-c740-e99b-e0e2-010a36ded92d@a-kretschmer.de/
Also on:    postgresql.org/message-id/f02993c7-c740-e99b-e0e2-010a36ded92d@a-kretschmer.de

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: andreas@a-kretschmer.de, pgsql-sql@lists.postgresql.org
  Subject: Re: Windowing ?
  In-Reply-To: <f02993c7-c740-e99b-e0e2-010a36ded92d@a-kretschmer.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox