pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedusing top-level aggregate values in subqueries
3+ messages / 3 participants
[nested] [flat]
* using top-level aggregate values in subqueries
@ 2001-04-24 00:26 Thomas F. O'Connell <tfo@monsterlabs.com>
0 siblings, 1 reply; 3+ messages in thread
From: Thomas F. O'Connell @ 2001-04-24 00:26 UTC (permalink / raw)
To: pgsql-sql
from the docs, i know that if you have two tables, foo and bar, you can
write a query such as
select f.bling
from foo f
where f.id = (
select max( b.id )
from bar b
where b.bling = "i kiss you!"
);
what i'm wondering is if you need that subquery in two places in a query
if there's some way to cache it at the top level.
for instance, if i were shooting for
select f.id
from foo f, ola o
where f.id = (
select max( b.id )
from bar b
where b.bling = "i kiss you!"
)
and o.id != (
select max( b.id )
from bar b
where b.bling = "i kiss you!"
)
is there some way to grab the value returned by the subquery in the
superquery and use the value instead of running the subquery twice?
i'm not looking for an optimized version of my example (unless it
answers the question of the bigger picture); i'd rather know if there's
some way to access top-level aggregates from within a subquery.
or find out that postgres is smart enough to recognize bits of SQL in a
query that are identical and do its own internal caching.
generically stated, my question is:
is there some way, without writing a function, to calculate an aggregate
value in a query that is used in multiple subqueries without needing to
run an aggregating query multiple times?
i know it only amounts to syntactic sugar, but, as such, it would be
pretty sweet.
thanks.
-tfo
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: using top-level aggregate values in subqueries
@ 2001-04-24 02:20 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas F. O'Connell <tfo@monsterlabs.com>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2001-04-24 02:20 UTC (permalink / raw)
To: Thomas F. O'Connell <tfo@monsterlabs.com>; +Cc: pgsql-sql
"Thomas F. O'Connell" <tfo@monsterlabs.com> writes:
> select f.id
> from foo f, ola o
> where f.id = (
> select max( b.id )
> from bar b
> where b.bling = "i kiss you!"
> )
> and o.id != (
> select max( b.id )
> from bar b
> where b.bling = "i kiss you!"
> )
> is there some way to grab the value returned by the subquery in the
> superquery and use the value instead of running the subquery twice?
In 7.1, perhaps something like this would do:
select f.id
from foo f, ola o,
(select max( b.id ) as max
from bar b
where b.bling = "i kiss you!") ss
where f.id = ss.max
and o.id != ss.max
In prior versions you'd have to fake it by selecting the subquery
result into a temp table beforehand.
> i'm not looking for an optimized version of my example
While it's not a general solution, there's always transitivity:
select f.id
from foo f, ola o
where f.id = (
select max( b.id )
from bar b
where b.bling = "i kiss you!"
)
and o.id != f.id
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: using top-level aggregate values in subqueries
@ 2001-04-24 04:31 Ossie J. H. Moore <ossie.moore@home.com>
0 siblings, 0 replies; 3+ messages in thread
From: Ossie J. H. Moore @ 2001-04-24 04:31 UTC (permalink / raw)
To: pgsql-sql
Subject: Re: [SQL] using top-level aggregate values in subqueries
Date: Mon, 23 Apr 2001 23:24:48 -0500
From: Ossie J. H. Moore <ossie.moore@home.com>
To: "Thomas F. O'Connell" <tfo@monsterlabs.com>
I'm a little unclear on what you are trying to do here so I'll take a stab at
explaining how you can compare two or more columns to the same columns in a
sub query...
1. Let's assume you have three tables:
CUSTomers (cust_id, cust_name),
MAGazines (mag_id, mag_name),
TERMs (cust_id, mag_id, mag_expire) where expire is an int2 representing the
year.
Values in the tables are as follows:
CUST: 1, JACK; 2, JILL; 3, JOE
MAG: 1, DOGS; 2, CATS
TERM: 1,1,2000; 1,1,2001; -- JACK subscribed to DOGS in 2000,2001
2,1,2000; -- JILL subscribed to DOGS in 2000
2,2,2001; -- JILL subscribed to CATS in 2001
3,2,2000; 3,2,2001; -- JOE subscribed to CATS IN 2000,2001
2. You want to see a list of customer and magazine name pairs of those
expiring in 2001.
SELECT C.CUST_NAME, M.MAG_NAME
FROM CUST C, MAG M
WHERE (C.CUST_ID, M.MAG_ID) IN
(
SELECT T.CUST_ID, T.MAG_ID
FROM TERM T
WHERE T.TERM_EXPIRE = 2001
)
ORDER BY C.CUST_NAME, M.MAG_NAME
3. For this specific situation, the better solution might have been the
statement below but the intent was to show how to match multiple columns in
your parent query to multiple columns in a sub query.
select c.cust_name
, m.mag_name
from cust c
, mag m
, term t
where c.cust_id = t.cust_id
and m.mag_id = t.mag_id
and t.term_expire = 2001;
On Monday 23 April 2001 19:26, you wrote:
> from the docs, i know that if you have two tables, foo and bar, you can
> write a query such as
>
> select f.bling
> from foo f
> where f.id = (
> select max( b.id )
> from bar b
> where b.bling = "i kiss you!"
> );
>
> what i'm wondering is if you need that subquery in two places in a query
> if there's some way to cache it at the top level.
>
> for instance, if i were shooting for
>
> select f.id
> from foo f, ola o
> where f.id = (
> select max( b.id )
> from bar b
> where b.bling = "i kiss you!"
> )
> and o.id != (
> select max( b.id )
> from bar b
> where b.bling = "i kiss you!"
> )
>
> is there some way to grab the value returned by the subquery in the
> superquery and use the value instead of running the subquery twice?
>
> i'm not looking for an optimized version of my example (unless it
> answers the question of the bigger picture); i'd rather know if there's
> some way to access top-level aggregates from within a subquery.
>
> or find out that postgres is smart enough to recognize bits of SQL in a
> query that are identical and do its own internal caching.
>
> generically stated, my question is:
>
> is there some way, without writing a function, to calculate an aggregate
> value in a query that is used in multiple subqueries without needing to
> run an aggregating query multiple times?
>
> i know it only amounts to syntactic sugar, but, as such, it would be
> pretty sweet.
>
> thanks.
>
> -tfo
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
-------------------------------------------------------
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-04-24 04:31 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-24 00:26 using top-level aggregate values in subqueries Thomas F. O'Connell <tfo@monsterlabs.com>
2001-04-24 02:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-04-24 04:31 Re: using top-level aggregate values in subqueries Ossie J. H. Moore <ossie.moore@home.com>
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