Re: Consecutive row count query

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Leon Stringer <leon(dot)stringer(at)ntlworld(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Consecutive row count query
Date: 2005-03-17 23:00:12
Message-ID: 87wts5j2tf.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Leon Stringer <leon(dot)stringer(at)ntlworld(dot)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)
WHERE b.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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Theo Galanakis 2005-03-17 23:32:48 Process priority.
Previous Message Jaime Casanova 2005-03-17 21:32:31 Re: Consecutive row count query