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 |
drdream
Starting Member
29 Posts |
Posted - 2007-04-08 : 06:21:13
|
How can I filter duplicate rows with more than one column in SQL Server?I have a table with every city in the USA likeCityID Cityname Zipcode------------------------------1 New York 100012 New York 100023 Boston 021154 Boston 021165 Boston 021136 Manchester 30012 I tried Select Distinct(CityName) but it returns all rows because of the CityID and Zipcode. I need the CityIDI would like to get (I dont need the zipcode at this point)--------------------------1 New York3 Boston6 Manchester More Info:This is inside a UDF Function which returns the closest cities (table) from a given lon/lat. The Cities Table has multiple listings for each city because of the zip codes. And also each city/zip pair has an ID.I was given this to try on yahoo answers:select * from citieswhere CityID in (select min(CityID) from CityIDgroup by Cityname) That actually does do the trick BUT for some reason its not returning the same cities as without it (seems to be skipping cities)and finally my UDFCREATE FUNCTION dbo.fnFindNearestCities( @Lat1 DECIMAL(10,6), @Lon1 DECIMAL(10,6))RETURNS TABLEASRETURN SELECT Miles = dbo.CalculateDistance(@Lat1, @Lon1, Lat, Lon), CityName, ZipPostal FROM US_Cities WHERE Miles IS NOT NULL GO Cities TableCREATE TABLE [US_Cities] ( [CityID] [int] IDENTITY (1, 1) NOT NULL , [CityName] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [CountryID] [int] NULL , [Rank] [int] NULL , [Lon] [float] NULL , [Lat] [float] NULL , [StateID] [int] NULL , [MajorCity] [bit] NULL , [NAC] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Population] [bigint] NULL , [ZipPostal] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [StateAbbr] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-08 : 09:30:37
|
[code]select min(CityID) as CityID, Cityname from US_Citiesgroup by Cityname[/code] KH |
 |
|
drdream
Starting Member
29 Posts |
Posted - 2007-04-08 : 13:43:09
|
Tried that but since Lat and Lon are used in the function, they have to be includedselect top 50 min(CityID) as CityID, Cityname, Miles = dbo.CalculateDistance(@Lat1, @Lon1, Lat, Lon) from US_Citiesgroup by Cityname Server: Msg 8120, Level 16, State 1, Line 31Column 'US_Cities.Lon' is invalid in the select list because it is not contained in either an aggregate function |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-08 : 18:03:07
|
[code]select CityID, CityNames, iles = dbo.CalculateDistance(@Lat1, @Lon1, Lat, Lon) from US_Cities cinner join ( select min(CityID) as CityID, Cityname from US_Cities group by Cityname) mon c.CityID = m.CityID[/code] KH |
 |
|
drdream
Starting Member
29 Posts |
Posted - 2007-04-09 : 02:26:22
|
This is the problem im having. Original query returns belowDECLARE @Lat1 DECIMAL(10,6)DECLARE @Lon1 DECIMAL(10,6)-- @Results intSET @Lat1 = 46.326160000000002SET @Lon1 = -96.528030000000001SELECT Distinct Top 20 CityName, CityID, Miles = dbo.CalculateDistance(@Lat1, @Lon1, Lat, Lon) --CityName --ZipPostal FROM US_Cities ORDER BY Miles------------------------------- -- ReturnsCityName CityID Miles ---------------------------------Boston 45001 .000Boston 45014 .560Boston 45028 .560Revere 51494 .560Chelsea 51493 .793Boston 45013 1.483Boston 45009 1.585Boston 45010 1.772Boston 51548 1.772Boston 51541 1.859Boston 51540 1.942Boston 51543 1.942Boston 51554 1.942Charlestown 45029 2.097Boston 45011 2.171Winthrop 51495 2.378Boston 45006 2.629Everett 51492 2.858Boston 45027 2.912Cambridge 51484 2.912------------------------------- New Query from khtan select top 20 c.CityID, c.CityName, miles = dbo.CalculateDistance(@Lat1, @Lon1, Lat, Lon) from US_Cities cinner join ( select min(CityID) as CityID, Cityname from US_Cities group by Cityname) mon c.CityID = m.CityIDOrder By Miles ReturnsCityID CityName Miles -----------------------------45001 Boston .00045029 Charlestown 2.09751478 Brighton 4.23151477 Allston 4.48451489 Brookline 5.34751491 Malden 5.46351480 Readville 5.90545026 Mattapan 6.24145030 Jamaica Plain 6.39044930 Nahant 6.63244928 Saugus 7.17744923 Lynn 7.49945031 Roslindale 7.66451510 Chestnut Hill 8.08351630 Newton Center 8.38844921 Winchester 8.51851632 Newton Highlands9.14145032 West Roxbury 9.19244915 Wakefield 9.19251633 Newton Lower Falls9.462 While khtan's query works at eliminating the duplicates, for some reason its skipping Revere, Chelsea, etc from the first function and they are closer in real life. Could it be the distance function? distance function code[code]ALTER FUNCTION dbo.CalculateDistance ( @Lat1 DECIMAL(10,6), @Lon1 DECIMAL(10,6), @Lat2 DECIMAL(10,6), @Lon2 DECIMAL(10,6)) RETURNS DECIMAL(12,3) AS BEGIN DECLARE @rads DECIMAL(10,8), @dist DECIMAL(12,3), @calc DECIMAL(10,8) SET @rads = 57.29577951 SELECT @lat1 = @lat1 / @rads, @lon1 = @lon1 / @rads, @lat2 = @lat2 / @rads, @lon2 = @lon2 / @rads IF @lat1 = @lat2 AND @lon1 = @lon2 SET @dist = 0.00 ELSE BEGIN SET @calc = SIN(@lat1) * SIN(@lat2) + COS(@lat1) * COS(@lat2) * COS(@lon1 - @lon2) IF (@calc) > 1.0 SET @calc = 1.0 SET @dist = 3963.1 * ACOS(@calc) END RETURN (@dist) END sorry for the long post! |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-09 : 02:33:24
|
[code]declare @US_Cities table( CityName varchar(20), CityID int, Miles decimal(6,3))insert into @US_Citiesselect 'Boston', 45001, .000 union allselect 'Boston', 45014, .560 union allselect 'Boston', 45028, .560 union allselect 'Revere', 51494, .560 union allselect 'Chelsea', 51493, .793 union allselect 'Boston', 45013, 1.483 union allselect 'Boston', 45009, 1.585 union allselect 'Boston', 45010, 1.772 union allselect 'Boston', 51548, 1.772 union allselect 'Boston', 51541, 1.859 union allselect 'Boston', 51540, 1.942 union allselect 'Boston', 51543, 1.942 union allselect 'Boston', 51554, 1.942 union allselect 'Charlestown', 45029, 2.097 union allselect 'Boston', 45011, 2.171 union allselect 'Winthrop', 51495, 2.378 union allselect 'Boston', 45006, 2.629 union allselect 'Everett', 51492, 2.858 union allselect 'Boston', 45027, 2.912 union allselect 'Cambridge', 51484, 2.912select top 20 c.CityID, c.CityName, Milesfrom @US_Cities cinner join ( select min(CityID) as CityID, CityName from @US_Cities group by CityName) mon c.CityID = m.CityIDOrder By Miles/*CityID CityName Miles ----------- -------------------- -------- 45001 Boston .00051494 Revere .56051493 Chelsea .79345029 Charlestown 2.09751495 Winthrop 2.37851492 Everett 2.85851484 Cambridge 2.912*/[/code] KH |
 |
|
|
|
|
|
|