Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 542509FB842 for ; Sun, 13 May 2007 10:47:55 -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 80464-03 for ; Sun, 13 May 2007 10:47:45 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from tinlc.com (unknown [72.18.206.65]) by postgresql.org (Postfix) with ESMTP id C47A79FB828 for ; Sun, 13 May 2007 10:47:50 -0300 (ADT) Received: from [127.0.0.1] (helo=localhost) by tinlc.com with smtp (Exim 4.60) (envelope-from ) id 1HnEPw-0001Jl-LF; Sun, 13 May 2007 08:47:20 -0500 From: "Greg Sabino Mullane" To: pgsql-sql@postgresql.org Subject: Re: Funny date-sorting task X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 X-Request-PGP: http://www.biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 In-Reply-To: <4646508B.5050404@gmx.net> Date: Sun, 13 May 2007 13:47:20 -0000 X-Mailer: JoyMail 2.01 Message-ID: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/98 X-Sequence-Number: 28378 -----BEGIN PGP SIGNED MESSAGE----- Hash: RIPEMD160 > Every task has a timestamp X that may be NULL or a date. > ... > 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 Assuming you mean the literal sense of "today", and that future dates show with the least furthest away first: SELECT * FROM yourtable ORDER BY CASE WHEN X::date = now()::date THEN 1 WHEN X::date < now()::date THEN 2 WHEN X IS NULL THEN 3 ELSE 4 END, CASE WHEN X::date-now()::date < 0 THEN now()-X ELSE X-now() END; - -- Greg Sabino Mullane greg@turnstep.com End Point Corporation PGP Key: 0x14964AC8 200705130942 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -----BEGIN PGP SIGNATURE----- iD8DBQFGRxZIvJuQZxSWSsgRAwrsAJ9HzZXzf3sQs0FVNSrhxN5UpGhc+wCcDygQ obe5G3b58+pXhqy4Ybh/OM8= =rJpn -----END PGP SIGNATURE-----