Re: Sql ORDER BY and ASC/DESC question - Mailing list pgsql-sql

From Gregory Stark
Subject Re: Sql ORDER BY and ASC/DESC question
Date
Msg-id 87tzkvbfr6.fsf@oxford.xeocode.com
Whole thread Raw
In response to Re: Sql ORDER BY and ASC/DESC question  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
Responses Re: Sql ORDER BY and ASC/DESC question  (Tom Lane <tgl@sss.pgh.pa.us>)
Double query  (PostgreSQL Admin <postgres@productivitymedia.com>)
List pgsql-sql
"A. Kretschmer" <andreas.kretschmer@schollglas.com> writes:

> am  Wed, dem 30.01.2008, um 11:35:51 +0100 mailte Jaroslav Sivy folgendes:
>> Hello everyone,
>> 
>> I have following problem: am using pl/sql functions to trigger some
>> sql code and i need to pass ORDER_BY column name and ASC/DESC sorting
>> order as an input parameters into that function and order the result
>> based on these input parameters.
>> 
>> The problem is, that the only way is to create query as "string" and
>> then execute it as "string".
>
> Right.
>> 
>> Is there any other way around how to avoid that "string query"?

If you're not concerned with the planner being able to find indexes to satisfy
these orderings (ie, you don't mind always doing a sort) you could do
something like:

ORDER BY CASE ? WHEN 1 THEN name ASCWHEN 2 THEN name DESCWHEN 3 THEN height ASCWHEN 4 THEN height DESCELSE id ASCEND

But to the planner this will be basically an opaque expression. It won't be
able to use any indexes on these columns. Also, incidentally you might want to
use text strings instead of integer labels.

--  Gregory Stark EnterpriseDB          http://www.enterprisedb.com Get trained by Bruce Momjian - ask me about
EnterpriseDB'sPostgreSQL training!
 


pgsql-sql by date:

Previous
From: "Bart Degryse"
Date:
Subject: Re: Sql ORDER BY and ASC/DESC question
Next
From: Tom Lane
Date:
Subject: Re: Sql ORDER BY and ASC/DESC question