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 16296
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
possible in MySQL (I am using 4.0.18-32 with PHP...
|
by: limbert |
last post by:
------=_NextPart_000_0001_01C34D7B.DFBF53C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
=20
I was reading the manual and it said that...
|
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 use it for some querying of the database.
The...
|
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 where col_2 matches any of the col_1 values.
...
|
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 to do on the C++ code side is break these out...
|
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 PurTyp for
example.
You could have ...
|
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
http://hannes.imos.net/query.txt
I'd like to use...
|
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 I take the same query and run it on a database...
|
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 t2.a, t2.b from where)
However, I get the error...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |