【问题标题】:Lag() function not working in mysql workbench 8.0Lag() 函数在 mysql workbench 8.0 中不起作用
【发布时间】:2021-02-12 11:11:24
【问题描述】:
SELECT created_at, grand_total, 
lag(grand_total,1) over (order by o.grand_total desc) as 'lag'
FROM sales_flat_order;

错误代码:1064。您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在 '(order by o.grand_total desc) 附近使用的语法作为 'lag' FROM sales_flat_order' 在第 2 行 0.016 秒

代码失败:over **(**order by o.grand_total desc)

【问题讨论】:

  • 你运行的是什么版本的mysql(不是workbench)?

标签: mysql sql mysql-error-1064


【解决方案1】:
【解决方案2】:

您可以使用相关子查询:

SELECT created_at, grand_total, 
       (select grand_total
        from sales_flat_order sfo2
        where sfo2.grand_total > sfo.grand_total
        order by sfo2.grand_total asc
        limit 1
       )
FROM sales_flat_order sfo;

请注意,降序排列的lag() 通常称为“领先”。

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2016-07-10
    • 2019-02-18
    • 2018-12-28
    • 2020-06-07
    相关资源
    最近更新 更多