pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gregory Stark <stark@enterprisedb.com>
To: A. Kretschmer <andreas.kretschmer@schollglas.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Sql ORDER BY and ASC/DESC question
Date: Wed, 30 Jan 2008 13:32:45 +0000
Message-ID: <87tzkvbfr6.fsf@oxford.xeocode.com> (raw)
In-Reply-To: <20080130104226.GF26490@a-kretschmer.de>
References: <aa457f86948348248e93c6fc38b4b1f6@pobox.sk>
	<20080130104226.GF26490@a-kretschmer.de>

"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 ASC
 WHEN 2 THEN name DESC
 WHEN 3 THEN height ASC
 WHEN 4 THEN height DESC
 ELSE id ASC
 END

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's PostgreSQL training!



view thread (6+ messages)  latest in thread

Message-ID: <87tzkvbfr6.fsf@oxford.xeocode.com>
Permalink:  ../87tzkvbfr6.fsf@oxford.xeocode.com/
Also on:    postgresql.org/message-id/87tzkvbfr6.fsf@oxford.xeocode.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: stark@enterprisedb.com, andreas.kretschmer@schollglas.com
  Subject: Re: Sql ORDER BY and ASC/DESC question
  In-Reply-To: <87tzkvbfr6.fsf@oxford.xeocode.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox