Let's say we have a time-series dataset of entities metadata imported into postgres table Stats
:
CREATE EXTENSION IF NOT EXISTS POSTGIS;
DROP TABLE IF EXISTS "Stats";
CREATE TABLE IF NOT EXISTS "Stats"
(
"time" BIGINT,
"id" BIGINT,
"position" GEOGRAPHY(PointZ, 4326)
);
And here is the samples of table:
SELECT
"id",
"time"
FROM
"Stats"
ORDER BY
"id", "time" ASC
id|time|
-- ----
1| 3|
1| 4|
1| 6|
1| 7|
2| 2|
2| 6|
3| 14|
4| 2|
4| 9|
4| 10|
4| 11|
5| 32|
6| 15|
7| 16|
The business requirement is to assign route-id to entities in this table, so when the time for each entity jump over 1 second
it means the new flight or route for that entity. the final result would like this for previous samples:
id|time|route_id|
-- ---- --------
1| 3| 1|
1| 4| 1|
1| 6| 2|
1| 7| 2|
2| 2| 1|
2| 6| 2|
3| 14| 1|
4| 2| 1|
4| 9| 2|
4| 10| 2|
4| 11| 2|
5| 32| 1|
6| 15| 1|
7| 16| 1|
And this would be the new summary table of the routes:
id|start_time|end_time|route_id|
-- ---------- -------- --------
1| 3| 4| 1|
1| 6| 7| 2|
2| 2| 2| 1|
2| 6| 6| 2|
3| 14| 14| 1|
4| 2| 2| 1|
4| 9| 11| 2|
5| 32| 32| 1|
6| 15| 15| 1|
7| 16| 16| 1|
So how this complex query should be constructed?
CodePudding user response:
with data as (
select *, row_number() over (partition by id order by "time") rn from Stats
)
select id,
min("time") as start_time, max("time") as end_time,
row_number() over (partition by id order by "time" - rn) as route_id
from data
group by id, "time" - rn
order by id, "time" - rn
https://dbfiddle.uk/?rdbms=postgres_9.5&fiddle=c272bc57786487b0b664648139530ae4
CodePudding user response:
Assuming you have table stats
in hand, the following query will create a table by assigning route_id
:
Query to assign route_id using recursive-cte
:
CREATE TABLE tbl_route AS
with recursive cte AS
(
SELECT id, prev_time, time, rn, rn AS ref_rn, rn AS route_id
FROM
(
SELECT
*,
lag(time) OVER(partition BY id ORDER BY time) AS prev_time,
row_number() OVER(partition BY id ORDER BY time) AS rn
FROM stats
) AS rnt
WHERE rn=1
UNION
SELECT rnt2.id, rnt2.prev_time, rnt2.time, rnt2.rn, cte.rn AS ref_rn,
CASE
WHEN abs(rnt2.time-rnt2.prev_time)<=1 THEN cte.route_id
ELSE cte.route_id 1
END AS route_id
FROM cte
INNER JOIN
(
SELECT
*,
lag(time) OVER(partition BY id ORDER BY time) AS prev_time,
row_number() OVER(partition BY id ORDER BY time) AS rn
FROM stats
) AS rnt2
ON cte.id=rnt2.id AND cte.rn 1 = rnt2.rn
)
SELECT id, time, route_id FROM cte;
Query to check if route_id assigned was correct:
select id, time, route_id
from tbl_route
order by id, time
Query to create new summary
table:
select id, min(time) as start_time, max(time) as end_time, route_id
from tbl_route
group by id, route_id
order by id, route_id, start_time, end_time
Recursive-CTE Query breakdown:
Since recursive cte has been used, the query may look messy. However, I tried to break it down as follows:
- There are 2 main queries getting appended using
UNION
, First one will assignroute_id
for start of each id, Second will do it for rest of the rows for each id rnt
andrnt2
has been created because we needROW_NUMBER
andLAG
values to achieve this- We joined cte and rnt2
recursively
to assignroute_id
by checking the difference in the time