From: Greg Stark <gsstark@mit.edu>
To: Leon Stringer <leon.stringer@ntlworld.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Consecutive row count query
Date: 17 Mar 2005 18:00:12 -0500
Message-ID: <87wts5j2tf.fsf@stark.xeocode.com> (raw)
In-Reply-To: <1111090884.22783.5.camel@localhost.localdomain>
References: <1111090884.22783.5.camel@localhost.localdomain>
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)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: gsstark@mit.edu, leon.stringer@ntlworld.com
Subject: Re: Consecutive row count query
In-Reply-To: <87wts5j2tf.fsf@stark.xeocode.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox