pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Keith Worthington <keithw@narrowpathinc.com>
To: PostgreSQL SQL <pgsql-sql@postgresql.org>
Subject: sum query
Date: Fri, 3 Dec 2004 16:33:40 -0500
Message-ID: <20041203213340.M8095@narrowpathinc.com> (raw)

Hi All,

I am trying to join three tables and sum the quantities.

The first table contains all of the possible items.  The second table contains
orders and the third table contains the items on each order.

For all items found in tbl_item I need the total quantity on open orders.  If
an item is not on any open order the quantity should be zero.

Can someone please explain the best way to do this?

tbl_item
id    | ...
------+...
AB12  | ...
CD34  | ...
EF34  | ...
GH12  | ...
JK56  | ...

tbl_order
order | closed |...
------+--------+...
    1 | false  |...
    2 | true   |...
    3 | true   |...
    4 | false  |...
    5 | false  |...

tbl_item
order | id    | quantity
------+-------+---------
    1 | AB12  | 10
    1 | CD34  |  5
    2 | CD34  |  3
    3 | EF34  |  2
    3 | GH12  | 20
    4 | GH12  |  4
    5 | AB12  |  5

id    | quantity
------+---------
AB12  | 15
CD34  |  5
EF34  |  0
GH12  |  4
JK56  |  0

Kind Regards,
Keith 


______________________________________________
99main Internet Services http://www.99main.com




view thread (4+ messages)  latest in thread

Message-ID: <20041203213340.M8095@narrowpathinc.com>
Permalink:  ../20041203213340.M8095@narrowpathinc.com/
Also on:    postgresql.org/message-id/20041203213340.M8095@narrowpathinc.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: keithw@narrowpathinc.com
  Subject: Re: sum query
  In-Reply-To: <20041203213340.M8095@narrowpathinc.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox