Solved

# help with my SQL Query, checking results against another table

Posted on 2013-01-17
371 Views
Here is my command, what it does is find all cities within X miles of an existing city.  It works just fine.

set @cmd = 'SELECT  Distinct zips.CityName, zips.ProvinceAbbr
FROM  dbo.postalcodes AS [zips]
INNER JOIN dbo.Calculateboundary(' + @StrLat + ', ' + @StrLon + ', ' + @Strradius + ', ''' + @unit + ''') AS [bounds]
ON 1=1
WHERE [zips].[Latitude] BETWEEN [bounds].[South] AND [bounds].[North]
AND [zips].[Longitude] BETWEEN [bounds].[West] AND [bounds].[East]
AND [zips].[Latitude] <> 0
AND [zips].[Longitude] <> 0
AND dbo.CalculateDistance(' + @StrLat + ', ' + @Strlon + ', [zips].[Latitude], [zips].[Longitude], ''' + @unit + ''') <= ' + @Strradius + '
AND [zips].[CityType] = ''D'' and [zips].[active]=''1'''

This command above returns all CITY and STATE within X mile radius. (CityName and ProvinceAbbr)

So here is what I want to do.  I have a table called CITIES and there is a column called "CITYOPEN" that is either a 1 or a zero.  It also has matching columns of CITY and STATE.  I want my command above to return a third column CITYOPEN and its corresponding value.  The only catch is, that if a CITY, STATE returned in my first command does NOT exist in table CITIES (i.e. NULL), then CITYOPEN = 1.

CITYOPEN = 0 only happens when CITY, STATE exist in table CITIES and it is specifically set to 0, otherwise the value is always 1.   Hope that makes sense!
0
Question by:arthurh88
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 20

Accepted Solution

TheAvenger earned 500 total points
ID: 38789609

LEFT JOIN CITIES ON ... (write condition here to join the tables, i.e. to find them where you need them)

and in the select add ISNULL(CITIES.CITYOPEN, 1) AS CITYOPEN

This will make a join and if found will return the value of CITYOPEN. If there is no match, it will take 1, the second parameter of ISNULL.

If I have understood everything correctly, that should be the whole query:

set @cmd = 'SELECT  Distinct zips.CityName, zips.ProvinceAbbr, ISNULL(CITIES.CITYOPEN, 1) AS CITYOPEN
FROM  dbo.postalcodes AS [zips]
INNER JOIN dbo.Calculateboundary(' + @StrLat + ', ' + @StrLon + ', ' + @Strradius + ', ''' + @unit + ''') AS [bounds]
ON 1=1
LEFT JOIN CITIES ON CITIES.City = [zips].City AND CITIES.State = [zips].State
WHERE [zips].[Latitude] BETWEEN [bounds].[South] AND [bounds].[North]
AND [zips].[Longitude] BETWEEN [bounds].[West] AND [bounds].[East]
AND [zips].[Latitude] <> 0
AND [zips].[Longitude] <> 0
AND dbo.CalculateDistance(' + @StrLat + ', ' + @Strlon + ', [zips].[Latitude], [zips].[Longitude], ''' + @unit + ''') <= ' + @Strradius + '
AND [zips].[CityType] = ''D'' and [zips].[active]=''1'''
0

Author Comment

ID: 38789647
you understood, and it worked perfectly!  thank you
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
###### Suggested Courses
Course of the Month6 days, 10 hours left to enroll