简体   繁体   English

postgis postgres计数和ST_Distance函数的按列分组

[英]postgis postgres count and group by column for ST_Distance function

This SQL produces the following: 该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"

This SQL produces the following: 此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

Basically, I want the result like this that groups by city and the number of times same city occurs. 基本上,我想要这样的结果,即按城市和同一城市发生的次数进行分组。 something like this 像这样的东西

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

Im not an expert on joins, subquery, would that help ? 我不是联接,子查询方面的专家,这会有所帮助吗? Thanks in advance. 提前致谢。

this worked for me: 这为我工作:

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

Simple, plain SQL without a sub-query: 没有子查询的简单普通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)'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM