对于ST_Distance函数

时间:2015-08-11 09:23:30

标签: sql postgresql count group-by postgis

此SQL生成以下内容:

SELECT city FROM travel_logs ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)')) 

"Tshopo"
"Tshopo"
"Mongala"
"Haut-Komo"

此SQL生成以下内容:

SELECT city, count(*) AS count FROM travel_logs GROUP BY travel_logs.start_point, city ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)')) 

"Tshopo";1
"Tshopo";1
"Mongala";1
"Haut-Komo";1

基本上,我想要这样的结果,按城市分组和同一城市的次数。像这样的东西

"Tshopo";2  <--- its summed up correctly
"Mongala";1
"Haut-Komo";1

我不是加入,子查询的专家,会有帮助吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

这对我有用:

select city, count(*) as count
from
(SELECT city FROM travel_logs ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'))
) as subquery_travel_logs_nearest group by city

答案 1 :(得分:0)

没有子查询的简单,纯SQL:

SELECT city, count(*)
FROM travel_logs
GROUP BY city
ORDER BY ST_Distance(start_point,
         ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'));