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 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
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 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
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
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 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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: limbert |
last post by:
------=_NextPart_000_0001_01C34D7B.DFBF53C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
| |