Re: can't get the order I want after inserting new rows - Mailing list pgsql-sql

From Richard Huxton
Subject Re: can't get the order I want after inserting new rows
Date
Msg-id 41C30CA2.4050203@archonet.com
Whole thread Raw
In response to Re: can't get the order I want after inserting new rows  (Marcus Claesson <m.claesson@student.ucc.ie>)
List pgsql-sql
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
BYname 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


pgsql-sql by date:

Previous
From: RobertD.Stewart@ky.gov
Date:
Subject: Re: Table History
Next
From: Marcus Krause
Date:
Subject: get branches & childs from database