我有一张这样的桌子:
id visited_time page visitor_id
1 2019-04-29T10:44:53.847014+02:00 1 1
2 2019-04-29T10:46:53.174894+02:00 1 3
3 2019-04-29T10:49:44.000390+02:00 2 1
18 2019-04-29T10:52:46.574140+02:00 2 3
19 2019-04-29T10:52:58.158146+02:00 3 1
20 2019-04-29T10:53:27.402038+02:00 1 9
25 2019-04-29T10:55:18.275441+02:00 2 9
54 2019-04-29T11:10:01.818343+02:00 1 13
72 2019-04-29T11:40:28.056813+02:00 2 13
访问者也将从第1页到第2页到第3页,依此类推(但可能会退出)。我想查找在每个页面上花费的平均时间。从逻辑上讲,这是唯一的visitor_id访问的页面1和页面2等之间的区别。
在postgres中有一种聪明的方法吗?
答案 0 :(得分:1)
您在这里:
/^Power $Names1$/
在线示例:https://dbfiddle.uk/?rdbms=postgres_11&fiddle=e64dd8862350b9357d9a4384937868c9
还请确保您在SELECT
page,
avg(visited_time_next - visited_time)
FROM
(
SELECT
page,
visited_time,
-- the time of the next page view by a certain visitor...
lead(visited_time) OVER (PARTITION BY visitor_id ORDER BY visited_time) AS visited_time_next
FROM visits_so_56097366
) AS tmp
GROUP BY page
ORDER BY page;
和visitor_id
上有一个索引,否则对于大量中间行,您将得到非常昂贵的排序: