473,385 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reuse of Subselects

Hi all,

if I have something like this:

SELECT column1,
(... complicated subselect ...),
column1 - (... same subselect as above ...)
FROM table1;

do I really have to rewrite the subselect a 2nd time if I need that
result in another column's expression?

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #1
3 2400
Depend in the exact query, you can do:

SELECT column1, x.c, column1-x.c
FROM table1, (... complicated subselect ...) as x;

The above may not work if they're correlated, so you can try:

SELECT column1, column2, column1-column2
FROM
(SELECT column1, (... complicated subselect ...) as column2
FROM table1);

Hope this helps,

On Tue, Feb 17, 2004 at 01:02:43PM +0100, Holger Marzen wrote:
Hi all,

if I have something like this:

SELECT column1,
(... complicated subselect ...),
column1 - (... same subselect as above ...)
FROM table1;

do I really have to rewrite the subselect a 2nd time if I need that
result in another column's expression?

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org
--
Martijn van Oosterhout <kl*****@svana.org> http://svana.org/kleptog/ If the Catholic church can survive the printing press, science fiction
will certainly weather the advent of bookwarez.
http://craphound.com/ebooksneitherenorbooks.txt - Cory Doctorow


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAMgd6Y5Twig3Ge+YRAuTFAKDKWtaonB+zOZIO+415Eo uziuPptACgq7is
xBSqRe0p0KpTz8/MrHnx8zQ=
=93WW
-----END PGP SIGNATURE-----

Nov 22 '05 #2
temporary tables work. Save the complicated
subselect in temporary table, following
queries just simple select on temp table.

Holger Marzen wrote:
Hi all,

if I have something like this:

SELECT column1,
(... complicated subselect ...),
column1 - (... same subselect as above ...)
FROM table1;

do I really have to rewrite the subselect a 2nd time if I need that
result in another column's expression?


--
P. J. "Josh" Rovero Sonalysts, Inc.
Email: ro****@sonalysts.com www.sonalysts.com 215 Parkway North
Work: (860)326-3671 or 442-4355 Waterford CT 06385
************************************************** *********************

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #3
On Tue, Feb 17, 2004 at 13:02:43 +0100,
Holger Marzen <ho****@marzen.de> wrote:
Hi all,

if I have something like this:

SELECT column1,
(... complicated subselect ...),
column1 - (... same subselect as above ...)
FROM table1;

do I really have to rewrite the subselect a 2nd time if I need that
result in another column's expression?


You should be able to use a join. The syntax won't be simpler, but you
might get nearly a 2 fold speed up.

Assuming that the subselect depends on the current row, you want to do
something like:

SELECT (
SELECT column1, complicated, column1 - complicated FROM
(complicated select AS complicated) AS comptable)
) FROM table1;

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

Nov 22 '05 #4

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

Similar topics

3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By...
0
by: Petre Agenbag | last post by:
Hi List Can anyone help me to do the following without the need of subselects: I have a table that contains rows where mostly, the only common field is the "name". The rest of the data...
0
by: Jacob Friis Larsen | last post by:
Which is fastest? This: SELECT id, title FROM questions LEFT JOIN users USING (userid) WHERE zipcode = 2000; or this: SELECT id, title FROM questions WHERE userid IN (SELECT userid FROM...
6
by: J Smith | last post by:
After doing some googling through the lists and such, I wasn't able to arrive at a solution for a problem I've run into recently. I've seen mention of similar symptoms, but my case seems different....
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
1
by: Mike Mascari | last post by:
This is a really trivial question, but I'm curious. Why do subselects in target lists behave differently than simple queries? Ex: create temporary table bar (key varchar(32) not null);...
0
by: Tarun Mistry | last post by:
Hi guys, I have posted this in 2 groups hoping to catch up on some difference knowledge. I am making an application in c# with a SQL 2000 back end. I am trying to use the new dataset available...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
2
by: querry | last post by:
Hi all, I am trying to write a SQL Querry that inserts rows into a table by selecting values from another tables. General Syntax for inserting values into a table from one or more tables is as...
1
by: nullGumby | last post by:
I'm trying to get a UNION of UserIDs from multiple subselects. The original query--which puts all the found UserIDs into separate columns, looks like this: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.