Gossamer Forum
Home : General : Databases and SQL :

Nesting question

Quote Reply
Nesting question
Hello everybody, I am writing two sql queries and having trouble connecting them together....

My first sql is
---------------------------------------

Select A.License_Number
From Auto A, Tire T
Where A.License_Number = T.License_Number
Group By A.License_Number, T.Current_Tire_Position
Having Count(*) >= 2
Order By A.License_Number;

This will give the answer:

License_Number
5OEC140


My second sql is
---------------------------------------
Select Count(T.Tire_Number)
From Auto A, Tire T
Where A.License_Number = T.License_Number
AND A.License_Number = '5OEC140';

This will give the answer:

Count(T.Tire_Number)
4


What i want to do is to use nesting to combine these two queries together to produce the following result:

License_Number
5OEC140
Count(T.Tire_Number
4

Please advise what to do. Thanks alot for helping.
Quote Reply
Re: [ryan_ng3] Nesting question In reply to
First thing we'd need to know is what DBMS you are using. Also, is License_Number a primary key for the Auto table? If so, you can thin out those queries quite a bit.

But, what it seems like you want to do is find all license numbers and tire counts where the number of tires in a single tire position is greater than two. Is this correct?
Quote Reply
Re: [ryan_ng3] Nesting question In reply to
Not sure but maybe UNION is what you are after?

If you are using MySQL you might want to check out: http://www.mysql.com/doc/en/UNION.html. I suppose it would also work in similar ways with otjer DBMS.

Good Luck,

Tongue Netmos