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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub Queries

Status
Not open for further replies.

LukeFilms

Programmer
Nov 18, 2005
4
US
I am trying to pull contacts based on a radius of zipcodes.

I can do this my manually putting the ZipCodes in the Queries fine.

select Company,FirstName,LastName,Phone,ZipCode
From dbo.t_contacts
Where Church = '1'and
ZipCode in ('13760','13761','13762','13763','13850','13851','13790','13737','13732')

But I want to keep this Cities and Zipcode Radius in another table and pull in from a sub query.

Something like this.
select Company,FirstName,LastName,Phone,ZipCode
From dbo.t_contacts
Where Church = '1'and
ZipCode in (Select NY_Endicott from t_radius)"

I have two tables:

Table 1 t_contacts
Table 2 t_radius

I have t_radius with one column now called NY_Endicott and

t_radius has a column called NY_Endicott and it is populated with
id NY_Endicott
1 -Zipcodes as below-

('13760','13761','13762','13763','13850','13851','13790','13737','13732')
 
I am assuming you have like a handful of ranges you are thinking of that are hard and fast and unflexible and you are just doing this to save time. I'm not advocating this approach, but I'll show you how to do what it is you are trying to do. I think a better table design for this would be for your t_radius table to use bit fields for the city names you are thinking of and simply use 1 or 0 to indicate if the zip falls into that zone.
So you would have:

ZipCode NY_Endicott NY_Bronx Whatever
13760 1 0 0
13761 1 1 0

Then you could write your query as follows:
Code:
select c.Company,c.FirstName,c.LastName,c.Phone,c.ZipCode
From dbo.t_contacts c inner join t_radius r 
on c.zip_code = r.zip_code
Where c.Church = '1' 
and r.NY_Endicott = 1



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top