Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 79C339FB327 for ; Tue, 20 Feb 2007 00:32:41 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 66486-10 for ; Tue, 20 Feb 2007 00:32:33 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 8123A9FB2EE for ; Tue, 20 Feb 2007 00:32:38 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l1K4Wa6f019667; Mon, 19 Feb 2007 23:32:37 -0500 (EST) To: "Phillip Smith" cc: pgsql-sql@postgresql.org Subject: Re: DISTINCT ON not working...? In-reply-to: <003f01c75474$d1ce3480$9b0014ac@wbaus090> References: <003f01c75474$d1ce3480$9b0014ac@wbaus090> Comments: In-reply-to "Phillip Smith" message dated "Tue, 20 Feb 2007 09:25:11 +1100" Date: Mon, 19 Feb 2007 23:32:36 -0500 Message-ID: <19666.1171945956@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.063 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200702/205 X-Sequence-Number: 27539 "Phillip Smith" writes: > To cut out all the details, the code that is causing the problem: > SELECT DISTINCT ON (ean) > code, > CASE WHEN ean IS NULL OR valid_barcode(ean) = false THEN > null ELSE ean END AS ean > FROM TMPTABLE > WHERE code NOT IN (SELECT code FROM stock_deleted) > AND ean IS NOT NULL Perhaps you've confused yourself by using "ean" as both an input and an output column name? I think that the "ean" in the DISTINCT ON clause will effectively refer to that CASE-expression, whereas the one in the WHERE clause is just referring to the underlying column (and thus making the IS NULL test in the CASE rather pointless). regards, tom lane