Re: Overriding natural order of query results for a subset

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Nolan <htfoot(at)gmail(dot)com>
Cc: Laura Smith <n5d9xq3ti233xiyif2vp(at)protonmail(dot)ch>, postgre <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Overriding natural order of query results for a subset
Date: 2021-05-29 16:55:54
Message-ID: 2118122.1622307354@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Nolan <htfoot(at)gmail(dot)com> writes:
> You probably need some kind order by case when .... else .... end clause,
> where the else clause deals with the non-VIPs, probably negating the need
> for a nulls last clause.

The idiomatic way to do this, assuming that you create an "is_vip bool"
field or some other way to identify VIPs accurately, is

ORDER BY is_vip DESC, last_name, first_name

relying on the fact that bool TRUE > bool FALSE.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laura Smith 2021-05-29 19:04:08 Re: Overriding natural order of query results for a subset
Previous Message Michael Nolan 2021-05-29 16:48:18 Re: Overriding natural order of query results for a subset