pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Peter Eisentraut <peter@pathwaynet.com>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Tricky SQL (?)
Date: Sun, 4 Jul 1999 14:59:07 +0300
Message-ID: <l03130304b3a4f6fcb282@[147.233.159.109]> (raw)
In-Reply-To: <Pine.LNX.4.10.9907021657001.17665-100000@uruguay.pathwaynet.com>

At 00:04 +0300 on 03/07/1999, Peter Eisentraut wrote:


> 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.

It seems that the current solution would be to create a temporary table,
where you dump the results of your internal query, and then do the external
select. Would be more efficient than doing it on the frontend (network
traffic and all).

Version 6.5 has a facility for naming temporary tables so that you don't
have to worry about exclusive names in a multiuser environment. If you are
using a previous version, you should worry about this only if more than one
person uses said query at a time. Otherwise you should use a preexisting
table, and lock it prior to the query, which would mean another user needs
to wait until the first user finishes the query.

All that said, the solution would be [I haven't tested]:

CREATE TEMP TABLE int_qry ( customer_nr int4, amount decimal(2) );

BEGIN;

INSERT INTO int_qry( customer_nr, amount)
SELECT
   customer_nr, amount
   FROM credits
UNION
   customer_nr, -amount
   FROM refunds;

SELECT customer_nr, sum(amount)
FROM int_qry
GROUP BY customer_nr
HAVING sum(amount) > 0;

END;

DROP TABLE int_qry;

I inserted the BEGIN and END just to assert that the two operations are in
fact a single operation. Since temporary tables only exist in one session,
there is little danger of multiple queries messing with each other's
results.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (3+ messages)

Message-ID: <l03130304b3a4f6fcb282@[147.233.159.109]>
Permalink:  ../l03130304b3a4f6fcb282@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03130304b3a4f6fcb282@[147.233.159.109]

 · 

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 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: herouth@oumail.openu.ac.il, peter@pathwaynet.com
  Subject: Re: [SQL] Tricky SQL (?)
  In-Reply-To: <l03130304b3a4f6fcb282@[147.233.159.109]>
  References: <Pine.LNX.4.10.9907021657001.17665-100000@uruguay.pathwaynet.com> <l03130304b3a4f6fcb282@[147.233.159.109]>

* 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