X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id D52CA3134A0 for ; Tue, 10 Jun 2003 04:43:02 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 22089-08 for ; Tue, 10 Jun 2003 01:42:52 -0300 (ADT) Received: from smtp804.mail.sc5.yahoo.com (smtp804.mail.sc5.yahoo.com [66.163.168.183]) by svr1.postgresql.org (Postfix) with SMTP id 8BFD6311A9D for ; Mon, 9 Jun 2003 12:01:51 +0000 (GMT) Received: from adsl-67-119-37-32.dsl.lsan03.pacbell.net (HELO theplague) (heavenstrash@sbcglobal.net@67.119.37.32 with login) by smtp-sbc-v1.mail.vip.sc5.yahoo.com with SMTP; 9 Jun 2003 12:01:51 -0000 Reply-To: From: "James Taylor" To: Subject: Coalesce/Join/Entries may not exist. Date: Mon, 9 Jun 2003 05:00:16 -0700 Message-ID: <000101c32e7e$b4353a10$0300a8c0@theplague> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Importance: Normal X-Archive-Number: 200306/69 X-Sequence-Number: 13827 I've got three tables, I'll shorten the columns down just so you get the idea: lists ----------- id|order_id list_results ------------ id|lid|total orders ------------ id|max All of the columns are int's. What I'm trying to do is something like: select (o.max-coalesce(sum(lr.total),0)) from orders o,list_results lr where lr.l_id in (select l.id from lists l, orders o where l.order_id=X and o.id=l.order_id) group by o.max This would, in theory, return a number which should be o.total-sum(lr.total) The problem is, there may not be any data in list_results OR lists regarding the order ID. If data from list_results.total exists, and is referencing lists.id, which in turn is referencing orders.id through lists.order_id, return o.max-lr.total. If data from list_results or lists DOESN'T exist, I would just want to go ahead and return orders.max. I was hoping the coalesce would be able to do this, but it doesn't. The subquery is in there because frankly I'm not sure how to do multiple left joins, which I think would have to exist. The easy way out for me here I think would be to make list_results.order_id and leave lists out of it, but then I'd have redundant data in two tables. Any suggestions on this one?