X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D7F309DCC0C for ; Mon, 13 Feb 2006 11:39:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 39248-07 for ; Mon, 13 Feb 2006 11:39:25 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id CEF439DCA77 for ; Mon, 13 Feb 2006 11:39:18 -0400 (AST) Received: from plouf.absolight.net (plouf.absolight.net [193.30.224.136]) by svr4.postgresql.org (Postfix) with ESMTP id 602815AF880 for ; Mon, 13 Feb 2006 15:39:24 +0000 (GMT) Received: from andromede.in.absolight.net (andromede.in.absolight.net [193.30.224.82]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by plouf.absolight.net (Postfix) with ESMTP id 0B3E24A9C0B; Mon, 13 Feb 2006 16:39:21 +0100 (CET) Date: Mon, 13 Feb 2006 16:39:12 +0100 From: Mathieu Arnold To: mario.splivalo@mobart.hr, pgsql-sql@postgresql.org Subject: Re: ORDER BY CASE ... Message-ID: <9693350566E722C466358E08@[192.168.9.47]> In-Reply-To: <1139844930.13054.23.camel@localhost.localdomain> References: <1139844930.13054.23.camel@localhost.localdomain> X-Mailer: Mulberry/4.0.4 (Win32) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200602/132 X-Sequence-Number: 24142 +-le 13/02/2006 16:35 +0100, Mario Splivalo a dit : | Am I misusing the ORDER BY with CASE, or, what? :) | | I have a table, messages, half dozen of columns, exposing here just | three of them: | | pulitzer2=# select id, "from", receiving_time from messages where | service_id = 20 order by case when 5=5 then 2 else 3 end desc limit 5; | id | from | receiving_time | --------+---------------+------------------------ | 869585 | +385989095824 | 2005-12-08 16:04:23+01 | 816579 | +385915912312 | 2005-11-23 17:51:06+01 | 816595 | +38598539263 | 2005-11-23 17:58:21+01 | 816594 | +385915929232 | 2005-11-23 17:57:30+01 | 816589 | +385912538567 | 2005-11-23 17:54:32+01 | (5 rows) | | | pulitzer2=# select id, "from", receiving_time from messages where | service_id = 20 order by case when 5=6 then 2 else 3 end desc limit 5; | id | from | receiving_time | --------+---------------+------------------------ | 869585 | +385989095824 | 2005-12-08 16:04:23+01 | 816579 | +385915912312 | 2005-11-23 17:51:06+01 | 816595 | +38598539263 | 2005-11-23 17:58:21+01 | 816594 | +385915929232 | 2005-11-23 17:57:30+01 | 816589 | +385912538567 | 2005-11-23 17:54:32+01 | (5 rows) | | | I tought I'd get differently sorted data, since in the first query I | said 5=5, and in second I said 5=6. Well, no, in the first, the result of the CASE is 2, and in the second 3, it means that for every line, it'll sort using "2" as value for the first, and "3" for the second query. -- Mathieu Arnold