Special grouping on sorted data.

From: Nicolas Beuzeboc <nicolasb(at)norchemlab(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Special grouping on sorted data.
Date: 2008-09-22 22:49:38
Message-ID: 48D82102.6090404@norchemlab.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I was looking for a simple solution to this problem. I can't find a way
to group on b and n by just collapsing sequential n's (identical n's
right next to each other) the sorting condition is the timestamp.

b | n | stamp
----------------------------------------
A | 1 | 2008-09-20 06:07:47.981445
A | 1 | 2008-09-20 06:08:13.294306
A | 1 | 2008-09-20 06:12:02.046596
A | 2 | 2008-09-20 06:12:26.267786
A | 2 | 2008-09-20 06:12:47.750429
A | 1 | 2008-09-20 06:13:12.152512
A | 2 | 2008-09-20 06:13:39.052528
A | 2 | 2008-09-20 06:14:12.875389
B | 1 | 2008-09-20 06:14:29.963352
B | 1 | 2008-09-20 06:14:52.247307
B | 3 | 2008-09-20 06:15:13.358151
B | 3 | 2008-09-20 06:15:44.307792
B | 3 | 2008-09-20 06:16:17.32131
B | 2 | 2008-09-20 06:16:44.030435
B | 2 | 2008-09-20 06:17:00.140907
C | 1 | 2008-09-20 06:17:50.067258
C | 1 | 2008-09-20 06:18:22.280218
C | 1 | 2008-09-20 06:18:41.661213
C | 1 | 2008-09-20 06:19:07.920327
C | 3 | 2008-09-20 06:19:26.166675
C | 2 | 2008-09-20 06:19:46.459439
C | 2 | 2008-09-20 06:20:04.634328

Here I give an example of the output I'm looking for, And I can find a
way to do that in crystal report, but I would like postgresql to send it
that way. If the next n is different create a new row.

b | n | min_stamp | max_stamp
---------------------------------------------------------------------
A | 1 | 2008-09-20 06:07:47.981445 | 2008-09-20 06:12:02.046596
A | 2 | 2008-09-20 06:12:26.267786 | 2008-09-20 06:12:47.750429
A | 1 | 2008-09-20 06:13:12.152512 | 2008-09-20 06:13:12.152512
A | 2 | 2008-09-20 06:13:39.052528 | 2008-09-20 06:14:12.875389
B | 1 | 2008-09-20 06:14:29.963352 | 2008-09-20 06:14:52.247307
B | 3 | 2008-09-20 06:15:13.358151 | 2008-09-20 06:16:17.32131
B | 2 | 2008-09-20 06:16:44.030435 | 2008-09-20 06:17:00.140907
C | 1 | 2008-09-20 06:17:50.067258 | 2008-09-20 06:19:07.920327
C | 3 | 2008-09-20 06:19:26.166675 | 2008-09-20 06:19:26.166675
C | 2 | 2008-09-20 06:19:46.459439 | 2008-09-20 06:20:04.634328

If I write a GROUP BY b,n there is going to be only two rows for b =
'A', if I use distinct on i get the same thing

is there an easy way to do that ?

Nicolas

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2008-09-23 03:40:17 Re: Special grouping on sorted data.
Previous Message Mike Toews 2008-09-22 21:56:36 Multi-line text fields