Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i5s5x-0003XX-Fb for pgsql-sql@arkaria.postgresql.org; Thu, 05 Sep 2019 13:46:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i5s5w-0005lj-8G for pgsql-sql@arkaria.postgresql.org; Thu, 05 Sep 2019 13:46:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i5s5w-0005lc-06 for pgsql-sql@lists.postgresql.org; Thu, 05 Sep 2019 13:46:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i5s5s-00014H-Kh for pgsql-sql@lists.postgresql.org; Thu, 05 Sep 2019 13:46:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x85DkU8x005161; Thu, 5 Sep 2019 09:46:30 -0400 From: Tom Lane To: Mike Martin cc: pgsql-sql@lists.postgresql.org Subject: Re: Question about WHERE CASE In-reply-to: References: Comments: In-reply-to Mike Martin message dated "Thu, 05 Sep 2019 12:04:39 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <5159.1567691190.1@sss.pgh.pa.us> Date: Thu, 05 Sep 2019 09:46:30 -0400 Message-ID: <5160.1567691190@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mike Martin writes: > I was always under the impression that a case expression could only be > on the right side of a where expression ie: > WHERE fieldname= > Is this a postgres extention, cant find any documentation on this SQL has always had two forms of CASE: you can do CASE WHEN boolean_expression1 THEN value1 [ WHEN boolean_expression2 THEN value2 ... ] [ ELSE valueN ] END or you can do CASE test_value WHEN comparison_value1 THEN value1 [ WHEN comparison_value2 THEN value2 ... ] [ ELSE valueN ] END The latter is effectively the same as CASE WHEN test_value = comparison_value1 THEN value1 [ WHEN test_value = comparison_value2 THEN value2 ... ] [ ELSE valueN ] END except test_value is only supposed to be evaluated once. This goes back at least as far as SQL-92. It is documented, see https://www.postgresql.org/docs/current/functions-conditional.html regards, tom lane