x
login about faq Site discussion (meta-askssc)

Removing Repeated Identical SubQueries From A Query?

Hi there,

I have a query where the subquery is identical and repeated in both the WHERE and the SELECT clauses. Is it possible to remove this repeation from querying TABLE2 twice?

SELECT CLIENT, 
(SELECT PUB FROM TABLE2 WHERE VALID = 1 AND DATE <= GETDATE()) AS PUB  
FROM TABLE1
WHERE SEND = 'Y'  
AND (SELECT PUB FROM TABLE2 WHERE VALID = 1 AND DATE <= GETDATE()) IS NOT NULL

TIA

more ▼

asked Nov 10 '11 at 09:36 AM in Default

David 2 1 gravatar image

David 2 1
314 30 37 43

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

That subquery does not seem to be correlated with the outer query. If there is more than one row where valid = 1 and that date is before today (also note that date is a reserved word in recent versions of SQL), It will return an error about subqueries returning more than 1 value.

But to directly answer your question, in recent versions of SQL Server (2005 and later) you could use a CTE to avoid repeating it. In SQL Server 2000, you could consider turning that subquery into a view.

Also, if that subquery is expected to return exactly one result, as the structure of your query implies, and you could use a script instead of a single query, you could just stuff the one value it returns into a variable and then use the variable in your main query.

more ▼

answered Nov 10 '11 at 10:22 AM

TimothyAWiseman gravatar image

TimothyAWiseman
14.3k 16 20 29

(comments are locked)
10|1200 characters needed characters left

This seems to be the case of poor query design. You must re-design it according to the logic. If I understand correctly, you want to make sure that there must be one row (and exactly one) from TABLE2 which seems to be a look-up table with having only one row for criteria VALID = 1....

To me your query should be like this

SELECT  CLIENT
,       PUB
FROM    TABLE1
        CROSS JOIN ( SELECT PUB
                     FROM   TABLE2
                     WHERE  VALID = 1 AND DATE <= GETDATE()
                            --AND PUB IS NOT NULL /* Is this required? It depends upon the logic */
                   ) AS TABLE2
WHERE   SEND = 1
more ▼

answered Nov 10 '11 at 11:27 PM

Usman Butt gravatar image

Usman Butt
13.8k 6 8 14

Thanks yes you are right it is poor design which I've inherited and looking to change. Your assumption are also correct in that there must and will only ever be one row returned. Away to test your code. Thanks.

Nov 11 '11 at 03:10 AM David 2 1
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x914
x454
x87
x45
x10

asked: Nov 10 '11 at 09:36 AM

Seen: 988 times

Last Updated: Nov 10 '11 at 09:36 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.