Re: ORDER BY

From: Alexander Staubo <alex(at)purefiction(dot)net>
To: MicroUser <a(dot)shafar(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ORDER BY
Date: 2006-11-15 21:35:35
Message-ID: DFDFB8FE-1787-4508-97C4-5FD12357936A@purefiction.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Nov 14, 2006, at 23:03 , MicroUser wrote:

> I need sorted result but the way like this:
>
> 0 | Anna
> 3 | Fernando
> 2 | Link
> 1 | Other
>
> Record '1 | Other' must by at the end of query result.

It's not apparent from your example that you want something other
than a purely lexicographic sort order (after all, "Other" comes
after "Link", "Fernando" and "Anna", so "order by name" already gets
you what you want), but I assume that's what you mean.

If your table is sufficiently small, and the complexity of the actual
query sufficiently low, prepending an expression sort key might suffice:

select * from foo
order by (case name when 'Other' then 1 else 0 end), name

Note that PostgreSQL is slow at evaluating case expressions, and this
might prove too slow. For larger tables, you may have to resort to a
union:

select * from foo where name != 'Other' order by name
union
select * from foo where name = 'Other'

Alexander.

In response to

  • ORDER BY at 2006-11-14 22:03:40 from MicroUser

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-11-15 21:51:41 Re: Using SAN Splits to instantly copy a DB
Previous Message Ron Johnson 2006-11-15 21:25:38 Re: Using SAN Splits to instantly copy a DB