Re: (P)SQL for a sum with constraints

Поиск
Список
Период
Сортировка
От A. Kretschmer
Тема Re: (P)SQL for a sum with constraints
Дата
Msg-id 20090415105648.GA7355@a-kretschmer.de
обсуждение исходный текст
Ответ на (P)SQL for a sum with constraints  (Shug Boabby <shug.boabby@gmail.com>)
Список pgsql-general
In response to Shug Boabby :
> 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
>

wait for 8.4:

test=# select * from shug ;
 a | b
---+---
 1 | 0
 2 | 1
 3 | 0
 4 | 2
 5 | 1
(5 rows)

test=# select a, sum(b) over (order by a) from shug ;
 a | sum
---+-----
 1 |   0
 2 |   1
 3 |   1
 4 |   3
 5 |   4
(5 rows)



Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

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

Предыдущее
От: Ow Mun Heng
Дата:
Сообщение: pg_reorg -> Anyone has any experience with it?
Следующее
От: Dimitri Fontaine
Дата:
Сообщение: Re: Disconnected editing - versioning of databases