?
Solved

help with my SQL Query, checking results against another table

Posted on 2013-01-17
2
Medium Priority
?
378 Views
Last Modified: 2013-01-17
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
Comment
Question by:arthurh88
2 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 2000 total points
ID: 38789609
Try to add this:

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

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question