我有一个名为包含这些行的位置的表:
id uuid NOT NULL,
"deviceId" text COLLATE pg_catalog."default",
"userId" uuid,
"userName" text COLLATE pg_catalog."default",
"creationDateTime" timestamp with time zone,
shape geometry,
CONSTRAINT id PRIMARY KEY (id)
想象一下,我的用户在shape
列中将每小时的点数记录到此表中。当注册登记到表中的那个点的时间时,保存到像creationDateTime
这样的2018-08-22 00:03:41.649+04:30
列中。
我如何提取此信息:
each User ---- each day ---- list of geometry(shape column)
在例子中:
第一天的User1具有几何点列表。第二天的User1有几何点列表,依此类推......
我通过mongo为同一个项目做了这个查询:
{$project: {
_id: 0,
uId : "$UserId",
dId : "$DeviceId",
ts :"$CreationDateTime",
point : "$Point"
}
},
{$group: {
_id :{
did: "$dId",
day: { $dayOfMonth: "$ts" }
},
docs: { $push: "$$ROOT" }
}
},
{
$sort:{"_id.day": -1}
}
但是我怎么能用postgresql做到这一点? postgre上没有这种聚合,我在postgresql上是新的。这是我的查询:
(Select test1."deviceId",test1."shape", test1."creationDateTime" From
(Select * from locations) as test1 Group By test1."deviceId",test1."shape",test1."creationDateTime"
ORDER BY test1."creationDateTime")
此查询没有合适的结果,我知道此查询有问题。用户的deviceId经常与其他行重复。我如何处理它?
最后,我想创建多折线per user - per day - multi poly line
可能有一百万种方法可以回答这个问题。这是其中之一:
考虑你的桌面结构..
CREATE TEMPORARY TABLE locations
(id uuid,
deviceId text COLLATE pg_catalog."default",
userId uuid,
userName text COLLATE pg_catalog."default",
creationDateTime timestamp with time zone,
shape geometry);
..以及这些样本数据..
INSERT INTO locations (userId, creationDateTime, shape)
VALUES ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-1.25 51.75)'),
('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-1.15 52.96)'),
('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-0.13 50.82)'),
('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE-1,'POINT(-2.22 53.48)'),
('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE-1,'POINT(-0.11 51.51)');
..你可以聚合每个用户的点数+日期,并使用LINESTRING
和ST_MakeLine
创建一个GROUP BY
:
SELECT userId, creationDateTime, ST_AsText(ST_MakeLine(shape))
FROM locations
GROUP BY userId, creationDateTime
ORDER BY creationDateTime;
userid | creationdatetime | st_astext
--------------------------------------+------------------------+-------------------------------------------------
d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-28 00:00:00+02 | LINESTRING(-2.22 53.48,-0.11 51.51)
d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-29 00:00:00+02 | LINESTRING(-1.25 51.75,-1.15 52.96,-0.13 50.82)
(2 Zeilen)
用户d1166a84-ab66-11e8-98d0-529269fb1459
在2018-08-28 00:00:00+02
的图形描述
以同样的方式,你可以使用MULTIPOINT
创建一个ST_Collect
:
SELECT userId, creationDateTime, ST_AsText(ST_Collect(shape))
FROM locations
GROUP BY userId, creationDateTime
ORDER BY creationDateTime;
userid | creationdatetime | st_astext
--------------------------------------+------------------------+-------------------------------------------------
d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-28 00:00:00+02 | MULTIPOINT(-2.22 53.48,-0.11 51.51)
d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-29 00:00:00+02 | MULTIPOINT(-1.25 51.75,-1.15 52.96,-0.13 50.82)
(2 Zeilen)
编辑 - 使用LINESTRINGS
(aka WITH Clause)为每个用户(MULTILINESTRING
)每天创建一组CTE
:
WITH j AS (
SELECT userId, creationDateTime, ST_MakeLine(shape) AS shape
FROM locations
GROUP BY userId, creationDateTime)
SELECT userId, ST_AsText(ST_Collect(shape))
FROM j
GROUP BY userId
userid | st_astext
--------------------------------------+----------------------------------------------------------------------------------
d1166a84-ab66-11e8-98d0-529269fb1459 | MULTILINESTRING((-2.22 53.48,-0.11 51.51),(-1.25 51.75,-1.15 52.96,-0.13 50.82))
(1
基本上你要做的就是对你需要的记录进行分组(在这种情况下是用户和日期)并使用你选择的聚合函数,例如: ST_MergeLine
,ST_Collect
,ST_Union
,ST_Multi
等。