【发布时间】:2018-07-27 11:53:40
【问题描述】:
我正在开发由IP2Location 提供的OpenSource 和免费数据库,其中列出了来自互联网的大量 IP 地址。
您可以从here下载。
我的主要兴趣不是 IP 地址:我希望每个国家/地区的每个城市都有 1 个地理坐标。
我现在的查询返回双倍:
WITH cte AS
(
-- Let's pass rows that have the same latitude and longitude
SELECT *, ROW_NUMBER() OVER (PARTITION BY latitude, longitude
ORDER BY latitude, longitude) AS rn
FROM ip2location_db11
-- Avoid rows without city name
where city_name != '-'
)
SELECT
-- These are the only columns I'm interested in from the whole ip2location_db11 database
ROW_NUMBER() OVER (ORDER BY country_code desc,city_name desc) as countdown_order,
latitude,longitude,city_name,country_code
FROM cte
-- Let's take the first row where latitude and longitude are the same
WHERE rn = 1
-- I want to order results by city name
order by countdown_order desc
这很烦人:
每个城市有一个纬度和经度的一行就可以了:我只想在地图上放一个大头针
【问题讨论】:
-
只需使用 row_number() over(partition by city_name order by countdown_order) 然后每个城市只会得到 1 行 - 在当前查询的外部选择中执行
标签: sql-server select subquery common-table-expression