我有一个相对较大的表,有很多列和行。 其中我有身份证,经度和纬度。 我想要一个具有相同坐标(纬度和经度)的ID列表 像这样的东西
ID¦latitude¦longitude¦number
1 ¦ 12.12¦ 34.54¦1
12¦ 12.12¦ 34.54¦1
52¦ 12.12¦ 34.54¦1
3 ¦ 56.08¦ -45.87¦1
67¦ 56.08¦ -45.87¦1
由于
答案 0 :(得分:2)
您可以使用EXISTS查询:
select *
from the_table t1
where exists (select 1
from the_table t2
where t1.id <> t2.id
and (t1.latitude, t1.longitude) = (t2.latitude, t2.longitude))
order by latitude, longitude;
或窗口函数:
select *
from (
select t.*,
count(*) over (partition by latitude, longitude) as cnt
from the_table t
) t
where cnt > 1
order by latitude, longitude;
答案 1 :(得分:1)
简单的解决方案:
SELECT
t.id, t.latitude, t.longitude, grp.tot
FROM
your_table t INNER JOIN (
SELECT latitude, longitude, count(*) AS tot
FROM your_table
GROUP BY latitude, longitude
HAVING count(*) > 1
) grp ON (t.latitude = grp.latitude AND t.longitude = grp.longitude);
或者获取lat / lng的重复项:
SELECT
latitude, longitude,
array_agg(id ORDER BY id) AS ids
FROM
place
GROUP BY
latitude, longitude
HAVING
count(*) > 1;