Re: (P)SQL for a sum with constraints

Поиск
Список
Период
Сортировка
От Shug Boabby
Тема Re: (P)SQL for a sum with constraints
Дата
Msg-id 9508491d0904170039r6ff1efceoa7d7730b46891fb0@mail.gmail.com
обсуждение исходный текст
Ответ на Re: (P)SQL for a sum with constraints  (Shug Boabby <shug.boabby@gmail.com>)
Список pgsql-general
Oh... and also, A, B, C are in the same table.

2009/4/17 Shug Boabby <shug.boabby@gmail.com>:
> Life sure would be easier if that were the case Michal, but no... that
> is not the case here. The sum is not a simple sum, it is a sum of all
> elements having a lower or equal A and the same C. This is a
> "cumulative sum" as pointed out by others.
>
> 2009/4/15 Michal Politowski <mpol+pg@meep.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;
>

В списке pgsql-general по дате отправления:

Предыдущее
От: Shug Boabby
Дата:
Сообщение: Re: (P)SQL for a sum with constraints
Следующее
От: Grzegorz Jaśkiewicz
Дата:
Сообщение: Re: Performance of full outer join in 8.3