Re: Consecutive row count query - Mailing list pgsql-sql

From Greg Stark
Subject Re: Consecutive row count query
Date
Msg-id 87wts5j2tf.fsf@stark.xeocode.com
Whole thread Raw
In response to Consecutive row count query  (Leon Stringer <leon.stringer@ntlworld.com>)
List pgsql-sql
Leon Stringer <leon.stringer@ntlworld.com> writes:

> Hi,
> 
> I wondered if anyone could answer the following question:
> 
> If I have a table such as the one below:
> 
> col1   col_order
> -----------
> Apple  1
> Apple  2
> Orange 3
> Banana 4
> Apple  5
> 
> Is there a way I can get the following results:
> 
> Apple  2
> Orange 1
> Banana 1
> Apple  1

Maybe. But not easily or efficiently.

How about this:

SELECT a.col1, a.col_order FROM tab as a LEFT OUTER JOIN tab as b    ON (b.col_order = a.col_order+1 AND
b.col1=a.col1)WHEREb.col1 IS NULL
 


> But since (in my intended table) most rows will have col_count = 1, this
> seems like unnecessary normalization (and semantically "wrong").

I think this looks like a better option. "unnecessary normalization" is an odd
phrase. Unless you can point at some reason that the denormalized seems *more*
convenient --and much *more* convenient at that-- not less convenient then you
should go for it.

Besides, that col_count column's only going to be four bytes. Unless the
"Apple" data is really short it'll only take a few col_count>1 to make it
worthwhile.

The only reason you might have a problem is if it's really "semantically
wrong" which would be if there's data attached to Apple or Orange that might
be different from one streak of results to the other.

-- 
greg



pgsql-sql by date:

Previous
From: Jaime Casanova
Date:
Subject: Re: Consecutive row count query
Next
From: Theo Galanakis
Date:
Subject: Process priority.