我想要得到时差
lead(time) - time
这不起作用:
SELECT
the_geom,
id,
lat,
lng,
mapid,
time,
lead(time) - time // THIS DOESNT WORK
OVER (PARTITION BY id ORDER BY time ASC)
as duration
FROM history
但这有效:
SELECT
the_geom,
id,
lat,
lng,
mapid,
time,
-time + lead(time) // THIS WORKS
OVER (PARTITION BY id ORDER BY time ASC)
as duration
FROM history
第一个给出错误:
Syntax error near OVER
我不知道为什么会这样,有人可以解释一下吗?
答案 0 :(得分:1)
窗口函数有两个部分
第一部分是类似于聚合函数的函数表达式,例如
Sum(revenue)
row_number()
rank()
此部分后面必须跟OVER clause
。在你的情况下
OVER (PARTITION BY id ORDER BY time ASC)
窗口功能是将两者放在一起,例如
SUM(revenue) OVER (PARTITION BY department
ORDER BY created_month
ROWS BETWEEN UNBUNDED PRECEDING AND CURRENT ROW)
-- this calculates the running total for fictitious `departments`
-- over months
你的第一个查询没有这样做,第二个查询没有,因为SQL在评估表达式时会忽略新行