X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 3D8679FA432 for ; Sun, 7 May 2006 11:20:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 93845-01 for ; Sun, 7 May 2006 11:20:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from coe.tamu.edu (edsun.coe.tamu.edu [165.91.232.6]) by postgresql.org (Postfix) with ESMTP id 77DFC9F9D82 for ; Sun, 7 May 2006 11:20:20 -0300 (ADT) Received: (from root@localhost) by bruner.coe.tamu.edu (8.13.1/8.13.1) id k47EEIgE012609; Sun, 7 May 2006 09:14:18 -0500 Received: from coe.tamu.edu (coe.tamu.edu [165.91.232.6]) by coe.tamu.edu (8.11.7p2+Sun/8.12.11) with ESMTP id k47EGfq14048; Sun, 7 May 2006 09:16:41 -0500 (CDT) Date: Sun, 7 May 2006 09:16:41 -0500 (CDT) From: "Ben K." To: pgsql-sql@postgresql.org Subject: Re: Most efficient way to hard-sort records In-Reply-To: Message-ID: References: <445C5A94.4020907@startnet.cz> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by AMaViS 0.3.12 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/78 X-Sequence-Number: 25109 > CREATE TABLE sorted (order_no SERIAL PRIMARY KEY, other columns...) > INSERT INTO sorted (columns) SELECT * FROM main_table INNER JOIN > key_table ON main_table.id = key_table.main_table_id WHERE key = 'param' ORDER > BY value SELECT > The SERIAL will automatically generate the order_no you want, which > corresponds to the position in the sorted set. > Then, to get the records in-order : > SELECT * FROM sorted ORDER BY order_no Good ... I just got myself into the habit of not recreating a table since I have to clean up permissions and what not. I guess it depends. Another version along that line ? # create sequence counterseq start 1; -- (set/reset whenever a counter is needed) # select main_table.*, nextval('counterseq') as position2 into sorted_main_table from main_table, keytable where main_table.id = keytable.main_table_id order by value; Regards, Ben K. Developer http://benix.tamu.edu