X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9B66F568B8 for ; Thu, 17 Mar 2005 23:36:08 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 20126-03 for ; Thu, 17 Mar 2005 23:36:07 +0000 (GMT) Received: from mail.libertyrms.com (unknown [207.219.45.62]) by svr1.postgresql.org (Postfix) with ESMTP id 4C3D6568AA for ; Thu, 17 Mar 2005 23:36:06 +0000 (GMT) Received: from ahammond-vpn.int.libertyrms.com ([10.1.7.26]) by mail.libertyrms.com with esmtp (Exim 4.22) id 1DC4XA-0002lK-Vj; Thu, 17 Mar 2005 18:36:09 -0500 Message-ID: <423A144B.2010304@ca.afilias.info> Date: Thu, 17 Mar 2005 18:35:39 -0500 From: Andrew Hammond User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Leon Stringer Cc: pgsql-sql@postgresql.org Subject: Re: Consecutive row count query References: <1111090884.22783.5.camel@localhost.localdomain> In-Reply-To: <1111090884.22783.5.camel@localhost.localdomain> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Mail-From: ahammond@ca.afilias.info X-SA-Exim-Scanned: No; SAEximRunCond expanded to false X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.239 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200503/252 X-Sequence-Number: 20731 You could hack it using a custom aggregate. NB: you'll want to reset the categorizer_seq every now and then. And this isn't safe for concurrent queries. You could make it safe for concurrent queries by using a complex type for STYPE, but I didn't bother. I also haven't debugged this, but I think it expresses the concept. CREATE SEQUENCE categorizer_seq; CREATE OR REPLACE FUNCTION categorizer_func (string, string) RETURNS bigint VOLATILE CALLED ON NULL INPUT AS ' SELECT CASE WHEN $1 = $2 THEN (SELECT last_value FROM categorizer_seq) ELSE nextval(''categorizer_seq'') END AS category ' LANGUAGE SQL; CREATE AGGREGATE categorizer ( BASETYPE = text, SFUNC = categorizer_func, STYPE = text, INITCOND = '' ); SELECT col1, count(*) FROM ( SELECT col1, cagetorizer(col1) AS category FROM mytable ORDER BY col_order ) tmp GROUP BY (col1, category); Leon Stringer wrote: > 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 > > i.e. Each row is printed ordered by col_order but consecutive > appearances of the same col1 result in only a single line in the result > with the number of consecutive appearances. > > Obviously I could store the table as: > > col1 col_order col_count > -------------------------- > Apple 1 2 > Orange 2 1 > Banana 3 1 > Apple 4 1 > > But since (in my intended table) most rows will have col_count = 1, this > seems like unnecessary normalization (and semantically "wrong"). > > Thanks in advance for any help, > > Leon Stringer > > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly