From: Rodrigo De León <rdeleonp@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Funny date-sorting task
Date: Sun, 13 May 2007 03:49:24 -0500
Message-ID: <a55915760705130149n58e6447av1c321840f98a8d9d@mail.gmail.com> (raw)
In-Reply-To: <5.2.1.1.0.20070512231137.042f88d0@pop6.sympatico.ca>
References: <4646508B.5050404@gmx.net>
<5.2.1.1.0.20070512231137.042f88d0@pop6.sympatico.ca>
On 5/12/07, Frank Bax <fbax@sympatico.ca> wrote:
> At 07:40 PM 5/12/07, Andreas wrote:
> >I've got a stack of tasks to show in a list.
> >Every task has a timestamp X that may be NULL or a date. It contains the
> >date when this tasks should be done.
> >Sometimes it has date and the time-part, too.
> >
> >
> >The list should be like this:
> >1) X sometime today should come first in ascending time order.
> >2) X in the past should show up after (1) in descending order so that
> >not so long back dates come first
> >3) X = NULL
> >4) X sometime in the future
> >
> >Could you provide a clever solution?
>
>
> ORDER BY CASE WHEN X=today THEN 1 ELSE
> CASE WHEN X<today THEN 2 ELSE
> CASE WHEN X IS NULL THEN 3 ELSE
> CASE WHEN X>today THEN 4 ELSE 5 END END END END
Less verbose:
ORDER BY CASE
WHEN x = today THEN 1
WHEN x < today THEN 2
WHEN x IS NULL THEN 3
WHEN x > today THEN 4
ELSE 5
END
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: rdeleonp@gmail.com
Subject: Re: Funny date-sorting task
In-Reply-To: <a55915760705130149n58e6447av1c321840f98a8d9d@mail.gmail.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