three-way join

From: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
To: pgsql-sql(at)postgresql(dot)org
Subject: three-way join
Date: 2004-04-19 12:47:28
Message-ID: 200404191347.28444.gary.stainburn@ringways.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi folks,

here's a straight forward join that I simply can't get my head round.

I've got

consumables: cs_id, cs_make, cs_comments
cons_locations: cl_id, cl_desc
cons_stock: cs_id, cl_id, status (1=ordered, 2=in-stock)

(one stock record per stock item, qty=3 means 3 records)

I'm struggling to create a quiery to produce

cs_id, cs_make, cs_comments, cl_desc, hand_qty, order_qty

where hand_qty and order_qty is the number of records grouped by cs_id, cl_id,
and status.

I've done the simple part and created a view balances to tally the cons_stock
as:

create view balances as
select cost_cs_id, cost_cl_id, cost_css_id, count(*) as qty
from cons_stock
group by cost_cs_id, cost_cl_id, cost_css_id;

I then have trouble joining this to the consumables and locations table to get
the results I need.
--
Gary Stainburn

This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Stark 2004-04-19 13:45:38 Re: Concatenate results of a single column query
Previous Message Bruno Wolff III 2004-04-19 03:21:57 Re: problem porting MySQL SQL to Postgres