ORDER BY 'DK', 'DE', DESC?

Started by Victor Spång Arthurssonalmost 22 years ago6 messagesgeneral
Jump to latest
#1Victor Spång Arthursson
scooterbabe@home.se

Hi!

I would like to know if it's possible to give a priority order of how
to sort the returning rows?

Like for example to order every row with a field language = DK first,
then the rows with field language = *DE' and last the other languages,
ordered alphabetically…?

Sincerely

Victor

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Victor Spång Arthursson (#1)
Re: ORDER BY 'DK', 'DE', DESC?

On Tue, 11 May 2004, [ISO-8859-1] Victor Spång Arthursson wrote:

Hi!

I would like to know if it's possible to give a priority order of how
to sort the returning rows?

Like for example to order every row with a field language = DK first,
then the rows with field language = *DE' and last the other languages,
ordered alphabetically…?

Well, I think you can do something like:

ORDER BY (language = 'DK'), (language = 'DE'), language

(or you could possibly condense the first two into one with case)

#3Adam Ruth
aruth@intercation.com
In reply to: Stephan Szabo (#2)
Re: ORDER BY 'DK', 'DE', DESC?

On May 20, 2004, at 11:20 AM, Stephan Szabo wrote:

On Tue, 11 May 2004, [ISO-8859-1] Victor Spång Arthursson wrote:

Hi!

I would like to know if it's possible to give a priority order of how
to sort the returning rows?

Like for example to order every row with a field language = DK first,
then the rows with field language = *DE' and last the other languages,
ordered alphabetically…?

Well, I think you can do something like:

ORDER BY (language = 'DK'), (language = 'DE'), language

(or you could possibly condense the first two into one with case)

Due to the sorting of boolean values, you'd need:

ORDER BY language = 'DK' desc, language like '%DE' desc, language;

Show quoted text

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

#4Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Adam Ruth (#3)
Re: ORDER BY 'DK', 'DE', DESC?

On Thu, 20 May 2004, Adam Ruth wrote:

On May 20, 2004, at 11:20 AM, Stephan Szabo wrote:

On Tue, 11 May 2004, [ISO-8859-1] Victor Spång Arthursson wrote:

Hi!

I would like to know if it's possible to give a priority order of how
to sort the returning rows?

Like for example to order every row with a field language = DK first,
then the rows with field language = *DE' and last the other languages,
ordered alphabetically…?

Well, I think you can do something like:

ORDER BY (language = 'DK'), (language = 'DE'), language

(or you could possibly condense the first two into one with case)

Due to the sorting of boolean values, you'd need:

ORDER BY language = 'DK' desc, language like '%DE' desc, language;

Yep, someday I'll remember that 0 is less than 1. ;)

#5Bruce Momjian
bruce@momjian.us
In reply to: Adam Ruth (#3)
Re: ORDER BY 'DK', 'DE', DESC?

Adam Ruth <aruth@intercation.com> writes:

Due to the sorting of boolean values, you'd need:

ORDER BY language = 'DK' desc, language like '%DE' desc, language;

Personally I find something like this clearer:

ORDER BY (CASE WHEN language = 'DK' THEN 1
WHEN language like '%DE' THEN 2
WHEN ...
ELSE 5
END
)

--
greg

#6Adam Ruth
aruth@intercation.com
In reply to: Stephan Szabo (#4)
Re: ORDER BY 'DK', 'DE', DESC?

On May 20, 2004, at 12:19 PM, Stephan Szabo wrote:

On Thu, 20 May 2004, Adam Ruth wrote:

On May 20, 2004, at 11:20 AM, Stephan Szabo wrote:

On Tue, 11 May 2004, [ISO-8859-1] Victor Spång Arthursson wrote:

Hi!

I would like to know if it's possible to give a priority order of
how
to sort the returning rows?

Like for example to order every row with a field language = DK
first,
then the rows with field language = *DE' and last the other
languages,
ordered alphabetically…?

Well, I think you can do something like:

ORDER BY (language = 'DK'), (language = 'DE'), language

(or you could possibly condense the first two into one with case)

Due to the sorting of boolean values, you'd need:

ORDER BY language = 'DK' desc, language like '%DE' desc, language;

Yep, someday I'll remember that 0 is less than 1. ;)

I only remember it after I try it once and wonder why my trues are at
the bottom!

Adam Ruth