Re: (P)SQL for a sum with constraints

From: Shug Boabby <shug(dot)boabby(at)gmail(dot)com>
To: Michal Politowski <mpol+pg(at)meep(dot)pl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: (P)SQL for a sum with constraints
Date: 2009-04-17 09:46:42
Message-ID: 9508491d0904170246p51f2f95csd02a3d96119fc665@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michal... I must apologise, your suggestion worked a treat!!!

I never realised it was possible to do a join on a table to itself before!

2009/4/15 Michal Politowski <mpol+pg(at)meep(dot)pl>:
> On Wed, 15 Apr 2009 15:32:42 +0100, Shug Boabby wrote:
>> I simplified my problem a little too much and now I'm stuck trying to
>> use cumulative_sum(). My schema is not only A, B but also has a C
>>
>> A B C
>> 1 0 1
>> 2 1 1
>> 3 0 1
>> 4 2 1
>> 5 1 1
>> 1 0 2
>> 2 1 2
>> 3 0 2
>> 4 2 2
>> 5 1 2
>>
>> and I want to be able to do the cumulative sum only when C is the same. E.g.
>>
>> A funkySumB C
>> 1 0 1
>> 2 1 1
>> 3 1 1
>> 4 3 1
>> 5 4 1
>> 1 0 2
>> 2 1 2
>> 3 1 2
>> 4 3 2
>> 5 4 2
>
> If I understand the problem correctly, why not just something like this?:
>
> SELECT t1.a, sum(t2.b), t1.c FROM abc t1 JOIN abc t2 ON t2.a <= t1.a AND
> t2.c=t1.c GROUP BY t1.a, t1.c;
>
> --
> Michał Politowski
> Talking has been known to lead to communication if practiced carelessly.
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bill Moran 2009-04-17 10:44:27 Re: Looking for advice on database encryption
Previous Message Chris.Ellis 2009-04-17 09:32:23 Re: Looking for advice on database encryption