X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 9376ED1E136 for ; Sat, 21 Feb 2004 08:21:22 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 05358-03 for ; Sat, 21 Feb 2004 04:21:03 -0400 (AST) Received: from 216.127.68.117 (unknown [216.127.68.117]) by svr1.postgresql.org (Postfix) with SMTP id 0BC0ED1D7FB for ; Sat, 21 Feb 2004 04:21:03 -0400 (AST) Received: (qmail 2353 invoked from network); 21 Feb 2004 08:21:03 -0000 Received: from m068-240.nv.iinet.net.au (HELO meerkatsoft.com) (203.217.68.240) by localhost with SMTP; 21 Feb 2004 08:21:03 -0000 Message-ID: <403714E3.2060600@meerkatsoft.com> Date: Sat, 21 Feb 2004 17:20:51 +0900 From: Alex User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en, ja MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: HELP with a Query Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/995 X-Sequence-Number: 57706 Hi, I need some help with a query. I use a reference table to lookup some codes. Everything works well if the reference table contains a reference but the query fails if no reference fails (Z.ref_code=123456). What I need to is either return 0 or null into Z.prim_exch if no reference can be found. Whats the best way to do that ? I tried a sub query but also did not succeed. SELECT A.sec_code,A.exch_code,A.price_date,Z.prim_exch, CASE WHEN A.exch_code = Z.prim_exch THEN 1 ELSE 0 END AS is_prim FROM price_hist AS A, references AS Z WHERE A.price_date <= '20040219' AND A.sec_code=123456 AND Z.ref_code=123456 LIMIT 10; Thanks for any help. Alex