Re: Help with sorting (ie. ORDER BY expression)

From: "Berend Tober" <btober(at)seaworthysys(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Help with sorting (ie. ORDER BY expression)
Date: 2005-02-08 15:22:22
Message-ID: 60266.216.238.112.88.1107876142.squirrel@216.238.112.88
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> This can be easily done with pl/pgsql, visit the documentation at
> http://www.postgresql.org/docs/7.3/interactive/programmer-pl.html
> OT: seems like this is a questionnaire/survey application, yes?
> - -----
> Jonel Rienton

FWIW, given the signature:

"Reuben D. Budiardja, Dept. Physics and Astronomy"

he's probably trying to utilize a data base to build an inventory of test
questions for the students he is responsible for actually teaching.

>> I am running postgres-7.3. I have a query like this:
>> SELECT question_id, question_text
>> FROM quiz_table
>> WHERE question_id IN (2,10,3,6,4,5);
>> But I want the output to be sorted in the way I give the question_id,
>> something like:
>> SELECT question_id, question_text
>> FROM quiz_table
>> WHERE question_id IN (2,10,3,6,4,5)
>> ORDER BY question_id (2,10,3,6,4,5)
>> Is there any way I can do that, so that the output of the query is
>> question_id, text
>> 2 ...
>> 10
>> 3
>> 6
>> 4
>> 5

Your understanding of the ORDER BY clause is off. My approach would be to add
a column "quiz_item_list_order", type integer, and explicity specify the rank
order in which you want questions to be returned.

SELECT quiz_item_list_order, question_id, question_text
FROM quiz_table
WHERE question_id IN (2,10,3,6,4,5)
ORDER BY quiz_item_list_order;

-- BMT

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Berend Tober 2005-02-08 15:30:37 Re: create aggregates to concatenate
Previous Message Berend Tober 2005-02-08 15:14:56 Re: Sorting when '*' is the initial character - solved