From: Chris Bitmead <chris@tech.com.au>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Tricky SQL (?)
Date: Sat, 03 Jul 1999 12:39:50 +1000
Message-ID: <377D77F6.8DCFAFC8@tech.com.au> (raw)
References: <Pine.LNX.4.10.9907021657001.17665-100000@uruguay.pathwaynet.com>
Use a temporary table.
Peter Eisentraut wrote:
>
> Let's say I have a table of credits with a customer number attached to it
> and a table of refunds with a customer number attached to it. Occasionally
> I want to go through this list and check if any customers still have
> more credits summed up than refunds.
>
> The technically correct choice for a query would be something like
>
> SELECT customer_nr, sum(amount) FROM ( SELECT customer_nr, amount FROM
> credits UNION customer_nr, -amount FROM refunds ) GROUP BY customer_nr
> HAVING sum(amount)>0;
>
> Unfortunately, this doesn't work because subselects are not allowed in the
> target list. The current solution is to read in all credits and refunds
> and have the application (some PHP, some Perl) do the summing and
> filtering. But this doesn't only seem clumsy but it creates unneccessay
> network traffic.
>
> Seemingly, this should be a common problem, like invoices vs. payments,
> assets vs. liabilities, etc. Does anyone have suggestions on how to tackle
> this? I am open to changes in the table structure, too.
>
> --
> Peter Eisentraut
> PathWay Computing, Inc.
--
Chris Bitmead
mailto:chris@tech.com.au
http://www.techphoto.org - Photography News, Stuff that Matters
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: chris@tech.com.au
Subject: Re: [SQL] Tricky SQL (?)
In-Reply-To: <377D77F6.8DCFAFC8@tech.com.au>
References: <Pine.LNX.4.10.9907021657001.17665-100000@uruguay.pathwaynet.com> <377D77F6.8DCFAFC8@tech.com.au>
* 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