pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: James Taylor <jtx@hatesville.com>
To: pgsql-sql@postgresql.org
Subject: Coalesce/Join/Entries may not exist.
Date: Mon, 9 Jun 2003 05:00:16 -0700
Message-ID: <000101c32e7e$b4353a10$0300a8c0@theplague> (raw)

I've got three tables, I'll shorten the columns down just so you get the
idea:

lists
-----------
id|order_id

list_results
------------
id|lid|total

orders
------------
id|max

All of the columns are int's.  What I'm trying to do is something like:

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=X and o.id=l.order_id)
group by o.max

This would, in theory, return a number which should be
o.total-sum(lr.total)

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?





view thread (2+ messages)  latest in thread

Message-ID: <000101c32e7e$b4353a10$0300a8c0@theplague>
Permalink:  ../000101c32e7e$b4353a10$0300a8c0@theplague/
Also on:    postgresql.org/message-id/000101c32e7e$b4353a10$0300a8c0@theplague

 · 

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: jtx@hatesville.com
  Subject: Re: Coalesce/Join/Entries may not exist.
  In-Reply-To: <000101c32e7e$b4353a10$0300a8c0@theplague>

* 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