我有一个带点几何的postgis表。
id | uid | date | geom
具有相同uid
的点数是相同的目标。我正在尝试使用ST_MakeLine GROUP BY uid
来创建完整的目标LineString。
SELECT uid, ST_MakeLine(geom)
FROM points
GROUP BY uid
这样可行,但我想确保这些点的顺序正确。
我尝试在分组之前添加ORDER BY date
来实现此目的。
SELECT uid, ST_MakeLine(geom)
FROM points
ORDER BY date <-- does not work
GROUP BY uid
ERROR: syntax error at or near "GROUP"
有没有办法在分组行输入聚合函数之前对其进行排序?
答案 0 :(得分:8)
ORDER BY
子句可以放在聚合参数的末尾。
SELECT uid, ST_MakeLine(geom ORDER BY date)
FROM points
GROUP BY uid
http://www.postgresql.org/docs/9.1/static/sql-expressions.html#SYNTAX-AGGREGATES
答案 1 :(得分:1)
也许你想在临时表中订购数据,然后通过uid进行外部分组?
SELECT uid, ST_MakeLine(geom)
FROM
(
SELECT uid, geom
FROM points
ORDER BY date
) AS temp
GROUP BY uid