Re: question about query - Mailing list pgsql-general

From David Johnston
Subject Re: question about query
Date
Msg-id 010b01cc37f3$56952fa0$03bf8ee0$@yahoo.com
Whole thread Raw
In response to Re: question about query  (Linos <info@linos.es>)
List pgsql-general
-----Original Message-----
i have created the union to get all the records (giving payments negative
sign) but what i don't know how to do it is use a window function to create
the column with the running total, any short example of syntax please?

>>>>>>>>>>

You'll need to check the documentation for the exact syntax but the
following should work as desired.

SELECT description, amount, transaction_date, SUM(amount) OVER (ORDER BY
transaction_date, amount)
FROM accountsreceivable

You do NOT use a "GROUP BY" in this instance because the "OVER" clause
following the SUM function turns it into a Window function which aggregates
in the context of a virtual window.  Using ORDER BY causes the SUM()
function to behave like a Running Sum.

You have not specified your version and Window functions are only available
in newer releases (>=8.4 I believe).  If you cannot use this then you will
have to write a store procedure.

David J.


pgsql-general by date:

Previous
From: AI Rumman
Date:
Subject: Re: pg_rman in Windows - is it possible?
Next
From: Adrian Klaver
Date:
Subject: Re: Anonymous record type and inner types.