X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1B12A329E5F; Fri, 24 Sep 2004 15:20:33 +0100 (BST) 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 76884-10; Fri, 24 Sep 2004 14:20:21 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id F36C7329CE3; Fri, 24 Sep 2004 15:20:24 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id i8OEKEfl021340; Fri, 24 Sep 2004 10:20:14 -0400 (EDT) To: Karsten Hilbert Cc: pgsql-general@postgresql.org, pgsql-sql@postgresql.org Subject: Re: need ``row number`` In-reply-to: <20040924092117.A1195@hermes.hilbert.loc> References: <20040924092117.A1195@hermes.hilbert.loc> Comments: In-reply-to Karsten Hilbert message dated "Fri, 24 Sep 2004 09:21:17 +0200" Date: Fri, 24 Sep 2004 10:20:13 -0400 Message-ID: <21339.1096035613@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/1040 X-Sequence-Number: 66246 Karsten Hilbert writes: > I am not convinced I'll need a SRF. I am not trying to > calculate something that isn't there yet. I am just trying to > join two views appropriately. I might have to employ some > variant of Celko's integer helper table but I'm not sure how > to proceed. A fairly common hack for this is to use a sequence: create temp sequence tseq; select nextval('tseq'), * from (select .... order by ...) ss; Note you must do any desired ORDER BY inside the subselect. If it's outside then it happens after computation of the row numbers, which is exactly not what you want. The major limitation of this is that you need to do some auxiliary operations to create or reset the semaphore before each query. There are some related hacks in the archives that use PL functions with private persistent state, instead of a sequence object. regards, tom lane