dbTalk Databases Forums  

Sorting based on maximum value over several columns

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Sorting based on maximum value over several columns in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Net Virtual Mailing Lists
 
Posts: n/a

Default Sorting based on maximum value over several columns - 11-06-2004 , 06:31 PM






Hello,

Lets say I have data like this:


value1|value2|value3|value4|....|value(N)
------|------|------|------|----|--------
100 | 200 | 300 | 400 | |
10 | 20 | | 40 | |
Quote:
15 | | 16 | |
5 | | | | |


Now I want to sort these based on the maximum value of the data in each
row, so for sorting purposes I would have this:

sort
----
400
40
16
5


Any ideas?... I've tried several things but none of them have given me
the result I am after....


Thanks as always!

- Greg


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #2  
Old   
Arjen van der Meijden
 
Posts: n/a

Default Re: Sorting based on maximum value over several columns - 11-07-2004 , 03:19 AM






Hi Greg,

Although it doesn't really seem to be a very well-structured database
design, I think there is a solution.

If the amount of fields is low, you can just stick a CASE in the MAX like:

SELECT ...,
MAX(
CASE WHEN value1 > value2 THEN
(CASE WHEN value1 > value3 THEN value1 ELSE value3 END)
ELSE
(CASE WHEN value2 > value3 THEN value2 ELSE value3 END)
END
) as sorter
FROM yourtable
GROUP BY ...
ORDER BY sorter

But if there are more than three values it will be a very long
CASE-statement and you're probably better off defining a FUNCTION. I'm
not sure whether it is possible to define a function with an unspecified
amount of inputvalues, but you can also use the table type as input type
and work with a table record in your function.
It might yield best performance, though, to create a C-function for this.

If there is already a "max of several fields"-function in PostgreSQL,
than you can use that of course.

Best regards,

Arjen

On 7-11-2004 1:31, Net Virtual Mailing Lists wrote:
Quote:
Hello,

Lets say I have data like this:


value1|value2|value3|value4|....|value(N)
------|------|------|------|----|--------
100 | 200 | 300 | 400 | |
10 | 20 | | 40 | |
| 15 | | 16 | |
5 | | | | |


Now I want to sort these based on the maximum value of the data in each
row, so for sorting purposes I would have this:

sort
----
400
40
16
5


Any ideas?... I've tried several things but none of them have given me
the result I am after....


Thanks as always!

- Greg


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.