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 494CFD1B4BE for ; Mon, 19 Apr 2004 11:06:34 -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 32064-02 for ; Mon, 19 Apr 2004 11:06:35 -0300 (ADT) Received: from mail.cobweb.nl (mx1.cobweb.nl [80.242.96.4]) by svr1.postgresql.org (Postfix) with ESMTP id 05614D1DC74 for ; Mon, 19 Apr 2004 11:06:30 -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 i3JE6gcg065459 for ; Mon, 19 Apr 2004 16:06:43 +0200 (CEST) (envelope-from s.vanroye@farcourier.com) MIME-Version: 1.0 Subject: Re: three-way join Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Mon, 19 Apr 2004 16:06:01 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message Message-ID: <71E201BE5E881C46811BA160694C5FCB0FA927@fs1000.farcourier.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] three-way join Thread-Index: AcQmEVCVlh6LX/2mSeyRZnlFCkZI7gABhlMA From: "Stijn Vanroye" To: "Gary Stainburn" , X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/161 X-Sequence-Number: 17536 Gary wrote: > Hi folks,=20 >=20 > here's a straight forward join that I simply can't get my head round. >=20 > I've got >=20 > consumables: cs_id, cs_make, cs_comments > cons_locations: cl_id, cl_desc > cons_stock: cs_id, cl_id, status (1=3Dordered, 2=3Din-stock) >=20 > (one stock record per stock item, qty=3D3 means 3 records) assuming that the PK's are: consumables : cs_id cons_loacations: cl_id cons_stock: cs_id, cl_id You could only have 1 record in cons_stock for each unique combination of c= onsumable and location.=20 If the primary key for cons_stock would also include the field status you c= ould have 2 records for each unique combination of consumable and location= , one where status is ordered, and one where status is in-stock. > I'm struggling to create a quiery to produce >=20 > cs_id, cs_make, cs_comments, cl_desc, hand_qty, order_qty >=20 > where hand_qty and order_qty is the number of records grouped=20 > by cs_id, cl_id,=20 > and status. Given the previous, the result for qty would be pretty obvious I think, sin= ce you would have only 1 record for the combination cs_id,cl_id and status. >=20 > I've done the simple part and created a view balances to=20 > tally the cons_stock=20 > as: >=20 > create view balances as=20 > select cost_cs_id, cost_cl_id, cost_css_id, count(*) as qty=20 > from cons_stock=20 > group by cost_cs_id, cost_cl_id, cost_css_id; I don't understand where the cost_* fields come from, especially the cost_c= ss_id field. Assuming that these fields are the cs_id, cl_id and status qty is most likl= ey going to be 1 all the time? Maybe it's worth to rethink your database structure, or adding the qty fiel= ds to the table cons_stock and keeping them up-to-date?=20 (eg. CONS_STOCK (cs_id, cl_id, hand_qty, order_qty) PK(cs_id, cl_id) ) that way you simply change the quantity fields for each combination of loca= tion-consumable according to the situation (and sound the alarm if the reac= h a certain level?). If anyone thinks I'm wrong, please correct me. Regards, Stijn Vanroye