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 8482B3CCE82 for ; Fri, 17 Dec 2004 16:43:31 +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 44794-01 for ; Fri, 17 Dec 2004 16:43:22 +0000 (GMT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by svr1.postgresql.org (Postfix) with ESMTP id 2033A3CCE65 for ; Fri, 17 Dec 2004 16:43:21 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 4.42) id 1CfLBa-000E0W-Ib; Fri, 17 Dec 2004 16:42:35 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 37AC31720C; Fri, 17 Dec 2004 16:43:14 +0000 (GMT) Message-ID: <41C30CA2.4050203@archonet.com> Date: Fri, 17 Dec 2004 16:43:14 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Marcus Claesson Cc: pgsql-sql@postgresql.org Subject: Re: can't get the order I want after inserting new rows References: <1103293951.17814.568.camel@morpheus.ucc.ie> In-Reply-To: <1103293951.17814.568.camel@morpheus.ucc.ie> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.086 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200412/129 X-Sequence-Number: 19749 Marcus Claesson wrote: > Hi Richard, > > >>Just looking at the start of your output, you are missing some rows >>(a/1/1) and have replaced others (a/2/1 isn't in your data). > > > Yes, that's exactly it. There's no mistake. The only column I need to > update is 'full', by giving every row a new 'full', based on its > 'score'. The tricky thing is that if a 'name' has several 'parts', then > all those 'parts' should have the same new 'full' value. Not sure if I'm > making much sense here...Just ask away if it's still unclear. OK - so if I re-arrange the columns: (name*, part*, score, full) The primary key is (name,part), (score) is what you measure and (full) is just for ordering. So - you want to update "full" so it reflects the sortorder something like: SELECT name, max(score) FROM table GROUP BY name ORDER BY max(score) DESC; You can then get the order you want with something like: SELECT t1.name, t1.part, t1.score, t2.order_score FROM table t1, ( SELECT name, max(score) AS order_score FROM table GROUP BY name ) AS t2 WHERE t1.name = t2.name ORDER BY t2.order_score DESC, t1.part ASC; Write a small plpgsql function to process the table in that order and update "full" accordingly. Actually, I'd add a SERIAL primary key and have a separate table for "full" - that way you can just delete the sorting values and replace them in one go. HTH -- Richard Huxton Archonet Ltd