0

我正在编写一个查询,该查询需要在与城市名称匹配的邮政编码数据库中查找不同的城市/州/国家/地区位置。这些列是:

Country, PostalCode, City, State, StateAbbr, County, CountryCode, Latitude, Longitude

结果集需要包含所有这些列。

以查询字符串:“Brooklyn”为例,我至少可以为每一行分配一个排名,如下所示:

select b.*,
  (select count(*) from PostalCodes b2 
   where b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country) as Rank
from PostalCodes b
where b.City LIKE 'Brooklyn%'

结果集有 66 行,其中 14 行是数据库中具有单个条目的位置,因此它们的排名为 1。其余 52 行是纽约布鲁克林的,它在数据库中有 52 个条目,并分配了排名52.

如何根据排名获取不同的行,并获取数据库中的所有列?我不在乎返回哪个邮政编码......我只想要每个城市/州/国家的一个条目。

我正在使用 SQL Server 2008。

4

4 回答 4

1

假设具有相同城市/州/国家/地区组合的每条记录在所有其他字段中具有相同的值,您可以使用此查询(尽管它有点小技巧):

SELECT b.*,
       PostalCode = (SELECT TOP 1 PostalCode FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country),
       StateAbbr = (SELECT TOP 1 StateAbbr FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country),
       County = (SELECT TOP 1 County FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country), 
       CountryCode = (SELECT TOP 1 CountryCode FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country), 
       Latitude = (SELECT TOP 1 Latitude FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country), 
       Longitude = (SELECT TOP 1 Longitude FROM PostalCodes b2 WHERE b.City = b2.City AND b.[State] = b2.[State] AND b.Country = b2.Country)

FROM(
SELECT DISTINCT CITY, STATE, COUNTRY
FROM PostalCodes
) b

您的请求的问题通常是当您按一组字段(在您的情况下为城市/州/国家/地区)分组时,您不包含任何其他列。这是一种解决方法,尽管它很慢并且可以优化。

于 2012-12-18T18:11:29.093 回答
1

这是另一个镜头。请注意,TOP 1 有效,因为您声明您不关心首先返回哪个特定行,并且在 Rank > 1 的情况下这将返回任意行。

SELECT  X.Rank, Y.*
FROM (  SELECT  B.City, B.State, B.Country, COUNT(*) AS [Rank]
        FROM    PostalCodes B
        GROUP BY B.City, B.State, B.Country
        ) X 
JOIN (SELECT ROW_NUMBER() OVER(PARTITION BY City, State, Country ORDER BY City, State, Country) AS RN,
        *
        FROM Postalcodes)
    Y
ON X.City = Y.City
AND X.State = Y.State
AND X.Country = Y.Country
WHERE Y.RN = 1              

这是另一个尝试。

请注意,查询性能将在很大程度上取决于可用的索引。我相信我以前使用过这个表,并且正如供应商提供的那样,索引对于这种查询并不是最佳的。

SELECT  RNK.Rank, PC.*
FROM    Postalcodes PC
JOIN (
    SELECT  MAX(PostalCode) as PostalCode,
            City, 
            StateAbbr, 
            CountryCode, 
            Count(*) AS Rank

    FROM    Postalcodes P
    group by city, stateabbr, countrycode
    ) RNK
ON  RNK.PostalCode = PC.PostalCode
AND RNK.City        = PC.City
AND RNK.StateAbbr   = PC.StateAbbr
AND RNK.CountryCode = PC.CountryCode
于 2012-12-18T18:46:14.150 回答
0
select COUNT(City),City,State,Country
from
 PostalCodes
 group by City,State,Country
 order by COUNT(City)
于 2012-12-18T18:09:57.820 回答
0

从您的查询中,您正在查找按城市、州和国家/地区的邮政编码计数。

我认为这会给你你正在寻找的东西:

SELECT  B.City, B.State, B.Country, COUNT(*) AS Rank
FROM    PostalCodes B
GROUP BY B.City, B.State, B.Country
于 2012-12-18T18:12:00.920 回答