【问题标题】:LEAD () Window Function not working in MYSQL WorkbenchLEAD () 窗口函数在 MYSQL Workbench 中不起作用
【发布时间】:2021-03-03 01:17:54
【问题描述】:
SELECT x,y,z,
LEAD(x) OVER(PARTITION BY y,z ORDER BY x) AS column
FROM Table;

这里的 X 是一个时间戳字段,这在 MYSQL Workbench 中不起作用。我收到以下错误 ( is not valid at this position, expecting EOF,';'
这里有什么问题?我错过了什么吗?

【问题讨论】:

  • 升级到更新版本的 MySQL。
  • 有什么解决方法可以在这个版本本身中达到相同的结果?
  • 对于给定的yzx 可以有重复值吗?

标签: mysql sql subquery mysql-workbench window-functions


【解决方案1】:

仅 MySQL 8.0 支持窗口函数。在早期版本中,一种解决方法是使用相关子查询:

select x, y, z,
    (select t1.x from mytable t1 where t1.y = t.y and t1.z = t.z and t1.x > t.x order by t1.x limit 1) as lead_x
from mytable t

这以某种方式假设没有重复 (x, y, z)(否则,结果可能会有所不同 - 但窗口函数的结果无论如何都不稳定)。

你也可以使用聚合而不是limit

select x, y, z,
    (select min(t1.x) from mytable t1 where t1.y = t.y and t1.z = t.z and t1.x > t.x ) as lead_x
from mytable t

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2016-07-10
    相关资源
    最近更新 更多