Removing the CASE statement all together: SELECT DISTINCT ON (ean) ean, count(*) FROM TMPTABLE WHERE code NOT IN (SELECT code FROM stock_deleted) AND ean IS NOT NULL GROUP BY ean
Still gives me: 3246576919422 2 -----Original Message----- From: Tom Lane [mailto:[EMAIL PROTECTED] Sent: Tuesday, 20 February 2007 15:33 To: Phillip Smith Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] DISTINCT ON not working...? 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 *******************Confidentiality and Privilege Notice******************* The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email. Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments ---------------------------(end of broadcast)--------------------------- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq