Sum raw with the same continuous flags

From: Shenli Zhu <zhushenli(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Sum raw with the same continuous flags
Date: 2013-06-01 12:43:44
Message-ID: CAA9w1xiLGDM9nuRr=_N8Roh46_fdrGDHy_huc=gi-S=bMquz2g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi, there are 2 columns, flag(boolean) and num(integer),in a table.
Table is like
| flag | num |
|------+-----|
| 1 | 2 | \ 5
| 1 | 3 | /
| 0 | 1 | \ 7
| 0 | 6 | /
| 1 | 4 | \ 9
| 1 | 5 | /
| ... | ... |
I want to sum up the raw with the same continuous flags. E.g. flag in 1st
and
2nd row are both 1, 3rd and 4th are both 0, 5th and 6th are both 1. So
the table becomes
| flag | num |
|------+-----|
| 1 | 5 |
| 0 | 7 |
| 1 | 9 |

Can I do this in SQL or PL/pgSQL? Any suggestions are welcome.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Kretschmer 2013-06-01 13:17:07 Re: Sum raw with the same continuous flags
Previous Message Pavel Stehule 2013-06-01 12:25:41 Re: Strange behavior of "=" as assignment operator