Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 053BB9FB43D for ; Mon, 19 Feb 2007 18:25:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 08824-02 for ; Mon, 19 Feb 2007 18:25:20 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from weatherbeeta.com.au (218-214-212-153.people.net.au [218.214.212.153]) by postgresql.org (Postfix) with SMTP id 72D179FB381 for ; Mon, 19 Feb 2007 18:25:22 -0400 (AST) Received: (qmail 24886 invoked from network); 19 Feb 2007 22:25:20 -0000 X-NetworkBox-HamSign: 0101;OUT;wbeetaau1;7d2bbde827fa148f77f6048b153515d5; Received: from mail.weatherbeeta.com.au (10.10.0.2) by network-box.weatherbeeta.com.au with SMTP; 19 Feb 2007 22:25:19 -0000 Received: (qmail 21866 invoked by uid 1166); 20 Feb 2007 09:25:15 +1100 Received: from 172.20.0.155 by mail.weatherbeeta.com.au (envelope-from , uid 1165) with qmail-scanner-1.25-st-qms (clamdscan: 0.88.6/2315. perlscan: 1.25-st-qms. Clear:RC:1(172.20.0.155):. Processed in 1.131689 secs); 19 Feb 2007 22:25:15 -0000 X-Antivirus-mail.weatherbeeta.com.au-Mail-From: phillip.smith@weatherbeeta.com.au via mail.weatherbeeta.com.au X-Antivirus-mail.weatherbeeta.com.au: 1.25-st-qms (Clear:RC:1(172.20.0.155):. Processed in 1.131689 secs Process 21845) Received: from it-phil.weatherbeeta.com.au (HELO wbaus090) (172.20.0.155) by mail.weatherbeeta.com.au with SMTP; 20 Feb 2007 09:25:12 +1100 From: "Phillip Smith" To: Subject: DISTINCT ON not working...? Date: Tue, 20 Feb 2007 09:25:11 +1100 Organization: Weatherbeeta Pty Ptd Message-ID: <003f01c75474$d1ce3480$9b0014ac@wbaus090> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 X-Scanned-By-wbeetaau1: Virus scan performed by network-box X-Scanned-By-wbeetaau1: Scanner file id is wbeetaau1-1171923919.941-24880-000 X-Scanned-By-wbeetaau1: No known viruses found in message (received+scanned in 0.02/0.08 secs) X-Scanned-By-wbeetaau1: Spam-Check-Result: No, hits=0 required=7 tests= autolearn=no version=2.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/204 X-Sequence-Number: 27538 Hi all, Strange one - I have a nightly export / import routine that exports from = one database and imports to another. Has been working fine for several = months, but last night it died on a unique constraint. 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) =3D false = THEN null ELSE ean END AS ean FROM TMPTABLE WHERE code NOT IN (SELECT code FROM stock_deleted) AND ean IS NOT NULL That is the code that generates the error on the unique constraint = against the ean column. If I play with that and run this: SELECT DISTINCT ON (ean) CASE WHEN ean IS NULL OR valid_barcode(ean) =3D false = THEN null ELSE ean END AS ean, count(*) FROM TMPTABLE WHERE code NOT IN (SELECT code FROM stock_deleted) AND ean IS NOT NULL GROUP BY ean I get a several thousand rows returned, all with a count(*) of 1, except = one row: 3246576919422 2 DISTINCT ON should eliminate one of those rows that is making that 2 - = as I said, it's been working fine for several months, and it is still doing = it correctly for approximately 100 other rows that have duplicate ean = codes. Can anyone give me a hand to work out why this one is doubling up?! Cheers, ~p *******************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