Solved

help with my SQL Query, checking results against another table

Posted on 2013-01-17
2
369 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

733 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