Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 8AB6C9FB2CA for ; Sun, 13 May 2007 05:49:29 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 33174-06 for ; Sun, 13 May 2007 05:49:23 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.250]) by postgresql.org (Postfix) with ESMTP id E15DC9FB2C4 for ; Sun, 13 May 2007 05:49:25 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id d17so335649and for ; Sun, 13 May 2007 01:49:24 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=JbfTOfaNFqhz5rVlZDWQG8NFAtkKsWnjIm79nchVdROI0CxDUyEvS1li9vq3cZUiKv20xbMkJVE881YXVDMxVamv+X/Y4DkdykKxYdzq1t905f09EeGPNnFadpkrV+vZNYbewZUSccRa+NAo2bA07FBmZuEnUMyjyOMg+EVDIGk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=eUlR8YG0Zi1jCbt2BHhMd1WwDQYQsk90MnCpzlEXsaJi/PuUi/95OsG5l6wfI+2QWR3Q0oliSe4yK07JpfHv98/JWOCpe0MJ3t+tUy83xYmWC6WhruFg1++OLX4QhrahZgv83Tk1itjkoY1r9ENkcPOZPga+OdksrfXnTVJr+Tk= Received: by 10.100.12.18 with SMTP id 18mr3575344anl.1179046164887; Sun, 13 May 2007 01:49:24 -0700 (PDT) Received: by 10.100.154.9 with HTTP; Sun, 13 May 2007 01:49:24 -0700 (PDT) Message-ID: Date: Sun, 13 May 2007 03:49:24 -0500 From: "=?ISO-8859-1?Q?Rodrigo_De_Le=F3n?=" To: pgsql-sql@postgresql.org Subject: Re: Funny date-sorting task In-Reply-To: <5.2.1.1.0.20070512231137.042f88d0@pop6.sympatico.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <4646508B.5050404@gmx.net> <5.2.1.1.0.20070512231137.042f88d0@pop6.sympatico.ca> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/97 X-Sequence-Number: 28377 On 5/12/07, Frank Bax 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 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