Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine these 2 Queries into 1 Query? 1

Status
Not open for further replies.

azstyx

Programmer
Oct 9, 2002
40
0
0
US
Hello Folks!
I am using 2 separate SQL queries to accomplish a task and I was wondering I can combine these 2 queries into one more efficient query?
My database table may have more than one entry if queried by Telephone number.
I only want to extract the record information only if there is ONE occurrence by telephone number.
If there is more than one record with the same telephone number - then I do nothing.
So I have a jscript code snippet and this is how I am doing it right now BUT I am wondering if I can do this effeciently in a single query?

This code uses 2 SQL queries to accomplish this:

strSQL = "SELECT COUNT(*) FROM tblTelDir WHERE TelNum = \"5555551212\"";
objRs = objDB.Execute(strSQL);
intNumOccurrances = objRs.Fields(0).Value; //get number of entries for tel num
if (intNumOccurrances == 1) {
strSQL = "SELECT DisplayName, BldAddr, Office FROM tblTelDir WHERE TelNum = = \"5555551212\"";
objRs = objDB.Execute(strSQL);
strDNUser = objRs("DisplayName").Value;
strLoc = objRs("BldAddr").Value;
strOffice = objRs("Office").Value;
}

Thanks!!
 
Code:
strSQL = "SELECT DisplayName,BldAddr,Office FROM tblTelDir WHERE TelNum='5555551212' AND 1=(SELECT COUNT(*) FROM tblTelDir WHERE TelNum='5555551212')";

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Try
SELECT DisplayName, BldAddr, Office
FROM tblTelDir
group by DisplayName, BldAddr, Office,TelNum
having count(*)=1
 
COOL!!!! Thanks to both of you!!!!!
:)
Styx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top