Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 02A6D9FBA66 for ; Wed, 14 Mar 2007 13:14:57 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 58026-06 for ; Wed, 14 Mar 2007 13:14:52 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.174]) by postgresql.org (Postfix) with ESMTP id 251FB9FB75A for ; Wed, 14 Mar 2007 13:14:50 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id k3so576922ugf for ; Wed, 14 Mar 2007 09:14:50 -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:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=HKuSWM2FFuC7Rlka9nuQyAksDATR7vIoOQktqVsw7cOR/+MXkjUOh6/GyD2PGHqIkGEopeXpy4tLjfHnpGrl81wMvV1cY0aqq5fKVyoQZPxijKlFnav85fQvLWl+is+Ks/te0VLVXVEqPi2G7kngZ0aH7ORzCdjL35OxZTbE3Cs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=cDN5PXcuFzNWPf/zMTDAM7qVb/tnZFcRLKAqQG1eMbkzK3UCqC4Bqt+ij3G1jjNowcvz5BV3qKKYlTy0VY3x/b35mxtWt/77DsI+a3yOKi1kgWbLJYxic9Ns92xzEF+pcu0Vp++W1kc6Wq3hDkBLNUi10KQWRrynVEB3qraL1QQ= Received: by 10.114.151.13 with SMTP id y13mr2966750wad.1173888889096; Wed, 14 Mar 2007 09:14:49 -0700 (PDT) Received: by 10.114.166.8 with HTTP; Wed, 14 Mar 2007 09:14:49 -0700 (PDT) Message-ID: Date: Wed, 14 Mar 2007 11:14:49 -0500 From: "=?ISO-8859-1?Q?Rodrigo_De_Le=F3n?=" To: pgsql-sql@postgresql.org Subject: Re: ordering by multiple columns Cc: "=?ISO-8859-1?Q?Pablo_Barr=F3n?=" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/201 X-Sequence-Number: 27839 On 3/14/07, Pablo Barr=F3n wrote: > Hi! > > I'm trying to order a list in which the first parameter to order is a > specific field, and the second should vary depending on a specific > condition. More explicit, I would like to do something like: > > - ORDER BY a.column1, [b.column2 if c.value !=3D 19 || c.column2 if > c.value=3D=3D19] > > That is to say, use a column if a value on each row isn't 19, OR use a > different column if it isn't. How could I solve this? I've got to have a > global order over my query, so I cannot separate it into two queries, etc > :-? > > Thank you! SELECT (...) ORDER BY a.column1, CASE WHEN c.VALUE <> 19 THEN b.column2 ELSE c.column2 END;