X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5DBF0D1BB89 for ; Tue, 20 Apr 2004 04:26:35 -0300 (ADT) 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 97409-04 for ; Tue, 20 Apr 2004 04:26:35 -0300 (ADT) Received: from mail.cobweb.nl (mx1.cobweb.nl [80.242.96.4]) by svr1.postgresql.org (Postfix) with ESMTP id 3F792D1B7F0 for ; Tue, 20 Apr 2004 04:26:32 -0300 (ADT) Received: from farcourier.com (dp3-26.dsl.cobweb.nl [193.67.120.154] (may be forged)) by mail.cobweb.nl (8.12.8/8.12.7) with ESMTP id i3K7Qbcg043256 for ; Tue, 20 Apr 2004 09:26:37 +0200 (CEST) (envelope-from s.vanroye@farcourier.com) MIME-Version: 1.0 Subject: Re: three-way join - solved Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Tue, 20 Apr 2004 09:26:03 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message Message-ID: <71E201BE5E881C46811BA160694C5FCB046723@fs1000.farcourier.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] three-way join - solved Thread-Index: AcQmKnYzQA05t7NiRTiSg2abetmJwQAfEFKw From: "Stijn Vanroye" To: "Gary Stainburn" , X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200404/185 X-Sequence-Number: 17560 First off, I'm sorry I couldn't get you a reply sooner. I see you have found another way, which is good :-) > I've sorted it.=20=20 >=20 > Firstly, I've done away with the status field. If an item's=20 > been issued or is=20 > still on order it does not have a location. I've therefore=20 > set up two=20 > locations, one of 'On Order' and one of 'Issued'. That's got=20 > rid of one=20 > table/relationship. It's also possible to include a status field in the consumables table, so you can give each consumable item it's own status directly. That way you won't have to perform a join operation to get the status. And/or you can filter directly on the consumables table, wich I think can have a performance benefit (no join nescessary). Neighter do you=20 need two location that aren't actually locations, which makes it easier to generate a list of (real) locations if nescescary. /*status field doesn't have to be a relation but a field which can only=20 contain 3 values (your statusses)).*/=20 But hey, your solution is just as good, you have to look at the situation at hand. >=20 > The relationship between the stock and the locations is=20 > simple. Each stock=20 > item has a location. I therefore do a straight forward join=20 > to end up with=20 > stock+location information. >=20 > The Stock->Consumable relation is just as simple. I do a=20 > straight forward=20 > join of the new stock+location data with the consumables=20 > data, and end up=20 > with what I need, consumable, location and quantity details, i.e.=20 >=20 > create view stock as > select c.*, b.cost_cl_id, b.cl_desc, b.qty from consumables c,=20 > (select b.*, cl.cl_desc=20 > from balances b, cons_locations cl=20 > where b.cost_cl_id =3D cl.cl_id=20 > order by cost_cs_id) b > where c.cs_id =3D b.cost_cs_id; >=20 > --=20 > Gary Stainburn For what it's worth here's a query that I think might work fine in you original situation, using your already created view: select bmain.cost_cs_id, consumables.cs_make, consumables.cs_comments,=20 cons_locations.cl_desc, b1.qty as hand_qty, b2.qty as order_qty=20 from=20 (select cost_cs_id, cost_cl_id from balances group by cost_cl_id, cost_cs_i= d) as bmain left join (select qty, cost_cs_id, cost_cl_id from balances where cost_cs= s_id=3D1) as b1=20 on (bmain.cost_cl_id=3Db1.cost_cl_id and bmain.cost_cs_id=3Db1.cost_cs_= id) left join (select qty, cost_cs_id, cost_cl_id from balances where cost_cs= s_id=3D2) as b2=20 on (bmain.cost_cl_id=3Db2.cost_cl_id and bmain.cost_cs_id=3Db2.cost_cs_= id) left join consumables=20 on (bmain.cost_cs_id=3Dconsumables.cs_id) left join cons_locations=20 on (bmain.cost_cl_id=3Dcons_locations.cl_id) P.S. Don't ask about performance of the query, I haven't delved that deep i= nto it :-) Regards and good luck, Stijn Vanroye