Solved

help with my SQL Query, checking results against another table

Posted on 2013-01-17
2
365 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 500 total points
Comment Utility
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
Comment Utility
you understood, and it worked perfectly!  thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now