473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clean up this simple query?

I am continually amazed by SQL's ability to humble me ....

I have a toy query into a toy database that looks just like this:

SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid and fw.wid = w.id and w.id in (((((select distinct w.id
from w where w.x in ('a', 'b') )))))
GROUP BY [f].[f]
HAVING count(f.id) = (select count(id) from (((((select distinct w.id from w
where w.x in ('a', 'b') ))))));
The query works and I am basically happy with it as it is, except it
contains the redundant string:

((((select distinct w.id from w where w.x in ('a', 'b'))))

duplicated precisely (I have marked it out with a few extra parens for easy
reading).
I need to do this query at runtime and would prefer not to do it twice ...
it just seems sloppy.

How can I eliminate the redundancy, perhaps by performing this query once
and storing the value to use a second time? Or does the optimizer do this
for me?

IOW, how can I do something like X = ((((select distinct w.id from w where
w.x in ('a', 'b')))), and then use X in place of the second evaluation of
this expression?

Thanks much.

Prefer reply to NG as my usual GMAIL account is flakey right now.

Thank you

RDeW

Nov 13 '05 #1
7 1918
You basically have 2 choices with SQL. You either duplciate the expression in
the query or you perform a query of a query to eliminate the duplication. In
either case, you're hoping the optimizer can do a good job with what you give
it, and the only way to find out for sure is to try it and/or look at a query
execution plan generated by the SQL processor.

On Sun, 28 Nov 2004 22:14:30 -0800, "Riley DeWiley" <ri***********@ gmail.com>
wrote:
I am continually amazed by SQL's ability to humble me ....

I have a toy query into a toy database that looks just like this:

SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid and fw.wid = w.id and w.id in (((((select distinct w.id
from w where w.x in ('a', 'b') )))))
GROUP BY [f].[f]
HAVING count(f.id) = (select count(id) from (((((select distinct w.id from w
where w.x in ('a', 'b') ))))));
The query works and I am basically happy with it as it is, except it
contains the redundant string:

((((select distinct w.id from w where w.x in ('a', 'b'))))

duplicated precisely (I have marked it out with a few extra parens for easy
reading).
I need to do this query at runtime and would prefer not to do it twice ...
it just seems sloppy.

How can I eliminate the redundancy, perhaps by performing this query once
and storing the value to use a second time? Or does the optimizer do this
for me?

IOW, how can I do something like X = ((((select distinct w.id from w where
w.x in ('a', 'b')))), and then use X in place of the second evaluation of
this expression?

Thanks much.

Prefer reply to NG as my usual GMAIL account is flakey right now.

Thank you

RDeW


Nov 13 '05 #2
"Riley DeWiley" <ri***********@ gmail.com> wrote in
news:10******** *****@corp.supe rnews.com:
I am continually amazed by SQL's ability to humble me ....

I have a toy query into a toy database that looks just like this:

SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid and fw.wid = w.id and w.id in (((((select distinct
w.id from w where w.x in ('a', 'b') )))))
GROUP BY [f].[f]
HAVING count(f.id) = (select count(id) from (((((select distinct w.id
from w where w.x in ('a', 'b') ))))));
The query works and I am basically happy with it as it is, except it
contains the redundant string:

((((select distinct w.id from w where w.x in ('a', 'b'))))

duplicated precisely (I have marked it out with a few extra parens for
easy reading).
I need to do this query at runtime and would prefer not to do it twice
... it just seems sloppy.

How can I eliminate the redundancy, perhaps by performing this query
once and storing the value to use a second time? Or does the optimizer
do this for me?

IOW, how can I do something like X = ((((select distinct w.id from w
where w.x in ('a', 'b')))), and then use X in place of the second
evaluation of this expression?

Thanks much.

Prefer reply to NG as my usual GMAIL account is flakey right now.

Thank you

RDeW


I can't think of any way to get rid of that "reduntant string". If the
optimizer can re-use that duplicate part is depending on what database
product you are using... (Don't expect too much.)

But there are some things you can do to simplify your query, and probably
improve performance a little.

Take a look at the first, "main" WHERE-clause:
WHERE f.id = fw.fid
and fw.wid = w.id
and w.id in (select distinct w.id
from w
where w.x in ('a', 'b'))

DISTINCT doesn't make any sense here. Remove it and you'll have
WHERE f.id = fw.fid
and fw.wid = w.id
and w.id in (select w.id
from w
where w.x in ('a', 'b'))
Then take a closer look at the HAVING-clause:
HAVING count(f.id) = (select count(id)
from (select distinct w.id
from w
where w.x in ('a', 'b')))

This can easily be rewritten as:
HAVING count(f.id) = (select count(distinct id)
from w
where w.x in ('a', 'b'))
Now your query is as:
SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid
and fw.wid = w.id
and w.id in (select w.id
from w
where w.x in ('a', 'b'))
GROUP BY "f"."f"
HAVING count(f.id) = (select count(distinct id)
from w
where w.x in ('a', 'b'))
If you know some details about the data, like unique constraints, you can
make the query even better. For example if the table w's id column is
unique, you can rewrite the query as:
SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid
and fw.wid = w.id
and w.x in ('a', 'b'))
GROUP BY "f"."f"
HAVING count(f.id) = (select count(distinct id)
from w
where w.x in ('a', 'b'))
HTH,
Jarl
Nov 13 '05 #3
Riley DeWiley wrote:
I am continually amazed by SQL's ability to humble me ....

I have a toy query into a toy database that looks just like this:

SELECT [f].[f]
FROM f, fw, w
WHERE f.id = fw.fid and fw.wid = w.id and w.id in (((((select distinct w.id
from w where w.x in ('a', 'b') )))))
GROUP BY [f].[f]
HAVING count(f.id) = (select count(id) from (((((select distinct w.id from w
where w.x in ('a', 'b') ))))));
The query works and I am basically happy with it as it is, except it
contains the redundant string:


My guess is that it is just Access that fouls up the representation of
the query. The optimizer in Jet will not evaluate this twice, I think.
You could try to store it as a separate query, but I can't tell if that
will expedite or hamper the query engine.
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #4
1) Please post DDL, so that people do not have to guess what the keys,
constraints, declarative referential integrity, datatypes, etc. In
your schema are. Sample data is also a good idea, along with clear
specifications.

2) Never name a column and a table the same thing. It is legal but
confusing and shows you have no data model -- a table is a set of
things and a column is a scalar, so you have confused a forest and a
tree.

3) Use and only one name for a column. You seem to be suffering from
the "magical, universal id" sdyndrome where the data element inherits
a table name when it is used somewhere else. Please read any book on
data modeling or ISO-11179 so you'll stop destroying your data
dictionary.

4) Why do think that excess parentheses will make the code easier to
read and maintain? It is not true. Why did you use needless
proprietary square brackets on names?

5) I am going to make a guess that the assorted "id" columns were
supposed to be named for a particular data element instead of a vague
universal attribute. I am going to use silly, but readable names for
tables. First hack:

SELECT Foobar.f
FROM Foobar AS F,
FooWoop AS FW,
Woop AS W
WHERE F.fid = FW.fid
AND FW.wid = W.wid
AND W.wid IN (SELECT W.wid
FROM Woop
WHERE x IN ('a', 'b'))
GROUP BY Foobar.f
HAVING COUNT(F.fid)
= (SELECT COUNT(DISTINCT W.wid)
FROM Woop
WHERE x IN ('a', 'b'));

This is gets rid of some of the query nesting. But since we have no
specs and no DDL, who knows??
How can I eliminate the redundancy, ... <<


1) Put it in a WITH clause, if you have an SQL that has implemented
that feature.

2) Let the optimizer handle it

3) Flatten out the query and get rid of the subqueries:

SELECT Foobar.f
FROM Foobar AS F,
FooWoop AS FW,
Woop AS W
WHERE F.fid = FW.fid
AND FW.wid = W.wid
AND W.x IN ('a', 'b')
GROUP BY Foobar.f
HAVING COUNT(F.fid)
= (SELECT COUNT(DISTINCT W.wid)
FROM Woop
WHERE x IN ('a', 'b'));

I am bothered by a COUNT(DISTINCT) on an idenrtifier column since it
shoudl be redundant. but without DDL, you knows??
Nov 13 '05 #5
AK
you haven't spe4cified RDBMS / version.
In general, google for "subquery factoring clause" / "WITH clause"
Nov 13 '05 #6
Just try the below query

select f.f
from f, fw, (select distinct w.id from w where w.x in ('a','b')) MyW
where f.id = fw.fid and
fw.wid = MyW.id
group by f.f
having count(f.id) = (select count(id) from MyW);
I haven't tested it.

Nov 13 '05 #7

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:18******** *************** **@posting.goog le.com...
<deletia>


Whatever. I am still thrashing over here. What I am looking for is an
implementation of the hangar problem in which the hangar and pilot tables
are joined by a junction table. I am building my hangar-analogue table on
the fly through a query into another table, all this in a db where the
"hangar" table is joined N:N with the "pilot" table through a bridge.

Make any sense? I think I can figure it out in about another three days or
so.

RDeW
Nov 13 '05 #8

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

Similar topics

8
4992
by: Craig Thomson | last post by:
I was wondering what people do with text provided by the user in a form. Some cleaning needs to be done at some stage if you are going to be putting it in a database or displaying it etc. But when is the time to do that? Do you clean it as soon as you get it? Do you pass around the original text and clean it when you use it? What about magic slashes? You need to addslashes before using in a db statement, but you need to strip them...
7
4400
by: Creative Acceleration | last post by:
Hi all, I need to convert the Query Strings into Clean URLs, Found some articles on PHP and Apache server.. How do we it them with ASP ?? Thanks Kart
2
8222
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly slowed down because five of my output fields are text fields and the expression that gets appended is a user-defined function I wrote which is very simple (just a few lines long). Here's the code of my function: Public Function tformat(num As...
10
4124
by: lallous | last post by:
Hello, This question was asked in comp.lang.c++ and the answers involved the use of objects whose destructors are automatically called when getting out of scope, however I was expecting suggestions that doesn't involve the use of objects. So here is the question again. I have a function like: void fnc() {
4
1807
by: Rick | last post by:
Hello, Below I create a static instance of an object, works well. public static BasicCounter GlobalCounter = new BasicCounter("GlobalCounter"); private void Page_Load(object sender, System.EventArgs e) { GlobalCounter.Increment();
5
1301
by: Rob | last post by:
I have a simple application consisting of about 4 forms and a few db connections... I would like to make sure that I have done proper clean up when the user exits the application. For all connections I have included a... cnn,Close() cnn.Dispose()
1
1765
by: KevinGPO | last post by:
I got ASP code (VBScript mixed with HTML & Javascript) and am looking for a clean/tidy program to clean up my code. Should I use Dreamweaver cleanup, tool?
4
1516
by: arak123 | last post by:
consider the following oversimplified and fictional code public void CreateInvoices(Invoice invoices) { IDbCommand command=Util.CreateDbCommand(); foreach(Invoice invoice in invoices) //lets say you have 200 invoices { command.CommandText+="INSERT INTO Invoice(Amount)
1
2166
by: sarahbanker | last post by:
ok, so I've inherited a nightmare of an Access 03 database, and trying to clean up the admin and maintainence side of it. It's being used as a tool to provide revised credit card limits based on gross income and UMI (uncommitted monthly income). I've tried various ways, but keep coming unstuck, so any help would be appreciated. Scenerio: Table Client Fields Client No UMI Gross Income ...
0
9666
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.