Search Postgresql Archives

(P)SQL for a sum with constraints

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello all,

I have a table with 2 bigint columns, let's call them A and B. I need
a query that will allow me to return A alongside the sum of Bs from
rows where A is less than or equal to this row's A. It is best
described with some example data, consider the following:

A B
1 0
2 1
3 0
4 2
5 1

I want to be able to make a query that returns the following rows:

A funkySumB
1 0
2 1
3 1
4 3
5 4

Anyone have any ideas how to do this? I'm able to do it
programmatically, but it's slow. Optimally I'd like to be able to do
this in the DB. As you can see, it's a little trickier that the usual
aggregate function with a GROUP BY and HAVING.

-- 
Shug

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux