Re: question about query

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Linos'" <info(at)linos(dot)es>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about query
Date: 2011-07-01 13:32:34
Message-ID: 010b01cc37f3$56952fa0$03bf8ee0$@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2011-07-01 13:36:26 Re: Anonymous record type and inner types.
Previous Message AI Rumman 2011-07-01 13:16:52 Re: pg_rman in Windows - is it possible?