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 1C6F530DED0 for ; Tue, 10 Jun 2003 11:57:13 +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 75050-06 for ; Tue, 10 Jun 2003 08:57:02 -0300 (ADT) Received: from mailav.tor1.inquent.com (mail.inquent.com [216.208.117.106]) by svr1.postgresql.org (Postfix) with ESMTP id 34F4630DDC3 for ; Tue, 10 Jun 2003 08:57:02 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mailav.tor1.inquent.com (Postfix) with ESMTP id ABCBD10258; Tue, 10 Jun 2003 07:51:02 -0400 (EDT) Received: from [192.168.1.200] (HSE-Toronto-ppp116801.sympatico.ca [216.209.81.112]) by mailav.tor1.inquent.com (Postfix) with ESMTP id 03A2710248; Tue, 10 Jun 2003 07:50:59 -0400 (EDT) Subject: Re: Coalesce/Join/Entries may not exist. From: Rod Taylor To: jtx@hatesville.com Cc: pgsql-sql@postgresql.org In-Reply-To: <000101c32e7e$b4353a10$0300a8c0@theplague> References: <000101c32e7e$b4353a10$0300a8c0@theplague> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-4y2/2oW185JSihE5N1WU" Organization: Message-Id: <1055246229.56597.37.camel@jester> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.4 Date: 10 Jun 2003 07:57:10 -0400 X-Virus-Scanned: by AMaViS perl-11 X-Archive-Number: 200306/79 X-Sequence-Number: 13837 --=-4y2/2oW185JSihE5N1WU Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Mon, 2003-06-09 at 08:00, James Taylor wrote: > I've got three tables, I'll shorten the columns down just so you get the > idea: >=20 > lists > ----------- > id|order_id >=20 > list_results > ------------ > id|lid|total >=20 > orders > ------------ > id|max >=20 > All of the columns are int's. What I'm trying to do is something like: >=20 > 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=3DX and o.id=3Dl.order_id) > group by o.max >=20 > This would, in theory, return a number which should be > o.total-sum(lr.total) >=20 > 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? Multiple left outer joins? FROM LEFT OUTER JOIN USING () LEFT OUTER JOIN USING () WHERE ... In your case,=20 SELECT (o.max - coalesce(sum(lr.total), 0)) FROM orders LEFT OUTER JOIN lists ON (orders.id =3D lists.order_id) LEFT OUTER JOIN list_results ON (lists.id =3D list_results.l_id) GROUP BY o.max But I'm not sure if that accomplishes what you're looking for or not. --=20 Rod Taylor PGP Key: http://www.rbt.ca/rbtpub.asc --=-4y2/2oW185JSihE5N1WU Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQA+5ceV6DETLow6vwwRAkzkAJ97IDW1R0+tqysnqE3LbKFmu/OoAQCbBBNb exSaa/NqlsA+xwrKFnOkmAs= =3B+K -----END PGP SIGNATURE----- --=-4y2/2oW185JSihE5N1WU--