472,330 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

is it possible to do this? have a subselect that returns two columns


So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z,
(SELECT a,b FROM foo) AS (sub_a,sub_b)
FROM tab

I don't think it's possible but it would simplify my life a whole heck of a
lot if it was, so I figured I would double-check before tearing my hair out.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #1
6 16219
You could try using it as a dynamic select as shown in the query below.
This would give you the answer by you would have to have a binding between
tab and the dynamic table z i believe

SELECT x,y,z,
z.a,
z.b
FROM tab,
(SELECT a,b FROM foo) z

HTH
Darren

On 3 Sep 2003, Greg Stark wrote:

So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z,
(SELECT a,b FROM foo) AS (sub_a,sub_b)
FROM tab

I don't think it's possible but it would simplify my life a whole heck of a
lot if it was, so I figured I would double-check before tearing my hair out.


--
Darren Ferguson
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #2
Ron
see below....

Greg Stark wrote:
So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z,
(SELECT a,b FROM foo) AS (sub_a,sub_b)
FROM tab

SELECT x, y, z, SS.*
FROM tab, (SELECT a,b FROM foo) SS

I don't think it's possible but it would simplify my life a whole heck of a
lot if it was, so I figured I would double-check before tearing my hair out.


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #3

da****@crystalballinc.com writes:

On 3 Sep 2003, Greg Stark wrote:
So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.


You could try using it as a dynamic select as shown in the query below.
This would give you the answer by you would have to have a binding between
tab and the dynamic table z i believe


What you describe as a "dynamic select" is more precisely a "view" and turns
the query into a join, which is what I explained I didn't want to do.

To give a better idea why I don't want to do it, try using that approach for a
more complex example:

SELECT x,y,z, count(*) as n
(select a,count(*) as b from foo where b.x=tab.x group by a) as (a,b),
(select c,count(g) as d from bar where c.y=tab.y group by c) as (c,d)
FROM tab
GROUP BY x,y,z

The only way to turn that into a join is to do make both views aggregates like
this:

SELECT x,y,z,count(*) as n, a,b,c,d
FROM tab
JOIN (select x,a,count(*) as b from foo group by x) AS foo USING (x)
JOIN (select x,c,count(g) as d from bar group by x) AS bar USING (x)
GROUP BY x,y,z

However as I showed in another thread, postgres will be incapable of using an
index on x to do this join, leading it to have to do a full seq scan of both b
and d and calculate the aggregates on the entire table. That's what I meant by
"it would become too complex and postgres would have trouble optimizing
things"

--
greg
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #4
On Wed, 2003-09-03 at 13:49, Ron wrote:
see below....

Greg Stark wrote:
So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z,
(SELECT a,b FROM foo) AS (sub_a,sub_b)
FROM tab


SELECT x, y, z, SS.*
FROM tab, (SELECT a,b FROM foo) SS


But where's the join between tab and foo? Wouldn't you then get
a combinatorial explosion?

--
-----------------------------------------------------------------
Ron Johnson, Jr. ro***********@cox.net
Jefferson, LA USA

"Perl is worse than Python because people wanted it worse."
Larry Wall, 10/14/1998
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #5
Ron
Ron Johnson wrote:
On Wed, 2003-09-03 at 13:49, Ron wrote:

see below....

Greg Stark wrote:
So I have a query in which some of the select values are subqueries. The
subqueries are aggregates so I don't want to turn this into a join, it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z,
(SELECT a,b FROM foo) AS (sub_a,sub_b)
FROM tab

SELECT x, y, z, SS.*
FROM tab, (SELECT a,b FROM foo) SS


But where's the join between tab and foo? Wouldn't you then get
a combinatorial explosion?

Oops, forgot to put the join in. And having re-read the original post I
can see that's what Greg was wanting to avoid. I'll just crawl back to
my corner now.........
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #6
So I have a query in which some of the select values are
subqueries. The
subqueries are aggregates so I don't want to turn this into a join,
it would
become too complex and postgres would have trouble optimizing things.

So my question is, is there some way to have a subselect return
multiple
columns and break those out in the outer query?

Something like:

SELECT x,y,z, (SELECT a,b FROM foo) AS (sub_a,sub_b) FROM tab


Assuming the select from foo only returns 1 row, see if this works for
you and can be planned effectively.

SELECT x, y, z, sub_a, sub_b
FROM (SELECT a,b FROM foo) t1(sub_a, sub_b),
(SELECT x, y, z FROM tab) t2

If a or b is aggregates and the foo subselect will return more than one
row (ie SELECT a , count(DISTINCT b) FROM foo GROUP BY a), then you
would need to have a JOIN field, or settle for a cartesian(sp?) product.

SELECT x, y, z, a, sub_b
FROM (SELECT a, sum(b) FROM foo GROUP BY a) t1(a, sub_b)
JOIN (SELECT a, x, y, z FROM tab) t2 USING(a)

hope this helps...
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phillip T. Murphy | last post by:
Well, after half pulling my hair out messing with this, I am thinking it is not possible. I did research and found references to "sub-queries" not...
0
by: limbert | last post by:
------=_NextPart_000_0001_01C34D7B.DFBF53C0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, ...
5
by: anthonyberet | last post by:
I work for an organisation that uses a bespoke document imaging system, the database of which is an MS sql server. We have MS Access and already...
14
by: php newbie | last post by:
I am getting error messages when I try to delete from a table using the values in the table itself. The intent is to delete all rows from TableA...
3
by: kjc | last post by:
I have a stored procedure what produces N number of rows. The rows are ordered by a cataegoryType as follows catA catB catC What is needed...
7
by: Patrick Fisher | last post by:
Hi I have a table which Contains entries with RefCode field containing INVP or INVPD Common fields in each entry would be InvoiceNo, Total and...
0
by: Hannes Dorbath | last post by:
First - I'm not sure whether this should go to .bugs, .hackers oder ..sql, so I posted here :/ The query and the corresponding EXPLAIN is at ...
0
by: eltontodd | last post by:
I have a query that I need to run on a database that is on a SQL Server 7 installation. When I run the query on that database it takes forever. If...
3
by: CodeButcher | last post by:
I have a subselect in my select statement: select t1.a, t1.b, (select top 1 t2.a from where) what I need is: select t1.a, t1.b, (select top 1...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.