Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
latitude38n
Starting Member
2 Posts |
Posted - 2004-03-12 : 21:09:52
|
I have the following code, which returns a list of nearby cities based on a city name as input. Most cities have multiple zipcodes per city name, thus it can list multiple rows with the same city name, but with different zipcodes like below:Zip | Cityname111 belmont112 belmont113 belmont114 san francisco115 san francisco---------------- etc----------------I do not really care about each group of zipcodes. I only need one pair of zipcode/city name like the following:ZIP | City name111 belmont114 San FranciscoHow do I change my select to only return a distinct city name. I do not care which if the city/zipcodes it returns from the similar city.The select statement is below:CREATE PROCEDURE ZipSearchByCity@city varchar(40),@State varchar(5),@distance int ASSELECT distinct o.City AS City, o.zip_code, o.State AS State, (3956 * (2 * ASIN(SQRT( POWER(SIN((z.RADlatitude-o.RADlatitude)/2),2) + COS(z.RADlatitude) * COS(o.RADlatitude) * POWER(SIN((z.RADlongitude-o.RADlongitude)/2),2) )))) distFROM zipcodes z, zipcodes o, zipcodes aWHERE z.city = @city ANDz.State = @State AND z.zip_code=a.zip_code AND (3956 * (2 * ASIN(SQRT( POWER(SIN((z.RADlatitude-o.RADlatitude)/2),2) + COS(z.RADlatitude) * COS(o.RADlatitude) * POWER(SIN((z.RADlongitude-o.RADlongitude)/2),2) )))) < @distanceGO Thanks,Christian |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-03-12 : 21:14:22
|
| SELECT MIN(ZIP), CitynameFROM Table1GROUP BY CitynameJust incorporate the above code into your select statement.Tara |
 |
|
|
latitude38n
Starting Member
2 Posts |
Posted - 2004-03-12 : 21:20:44
|
| When I include that code, I get quite a few (7) group by, aggregate clause errors. I do not think that I should group by all the RaDLongititude, etc in the code. Any ideas? |
 |
|
|
ChrisFretwell
Starting Member
43 Posts |
Posted - 2004-03-15 : 11:10:26
|
| Any field you return must be part of the grouping or a function within the group (min/max/avg etc). What is unique within each row, thats what you want to group by. Your question said you wanted to return (any)zip plus a city, which Tara's code did. You need to expand that to include the state and possibly the expression that you call 'dist'. If dist is unique for each city/state combination, then you group by it, if its not the same then you need to decide if you want the min/max value of it and included it that way. Below, I took your stored proc and put what would make sense for the group by if dist is unique by city.CREATE PROCEDURE ZipSearchByCity@city varchar(40),@State varchar(5),@distance int ASSELECT o.City AS City, min(o.zip_code), o.State AS State, (3956 * (2 * ASIN(SQRT( POWER(SIN((z.RADlatitude-o.RADlatitude)/2),2) + COS(z.RADlatitude) * COS(o.RADlatitude) * POWER(SIN((z.RADlongitude-o.RADlongitude)/2),2) )))) distFROM zipcodes z, zipcodes o, zipcodes aWHERE z.city = @city ANDz.State = @State AND z.zip_code=a.zip_code AND (3956 * (2 * ASIN(SQRT( POWER(SIN((z.RADlatitude-o.RADlatitude)/2),2) + COS(z.RADlatitude) * COS(o.RADlatitude) * POWER(SIN((z.RADlongitude-o.RADlongitude)/2),2) )))) < @distancegroup by o.City ,o.State , (3956 * (2 * ASIN(SQRT( POWER(SIN((z.RADlatitude-o.RADlatitude)/2),2) + COS(z.RADlatitude) * COS(o.RADlatitude) * POWER(SIN((z.RADlongitude-o.RADlongitude)/2),2) )))) |
 |
|
|
|
|
|
|
|