[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

help with my SQL Query, checking results against another table

Posted on 2013-01-17
2
Medium Priority
?
377 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
[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
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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…

656 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