mysql - 为什么 MySQL 中没有无限跟随?

标签 mysql sql window-functions

为什么我不能在 MySQL 中使用无限跟随

我可以使用 unbounded preceding 而不会对同一查询产生任何问题。

SELECT deptno,
       ENAME
       SAL,
       HIREDATE,
       last_value(HIREDATE) OVER (
           PARTITION BY deptno
           ORDER BY HIREDATE
           ROWS UNBOUNDED FOLLOWING
       )
FROM emp2
ORDER BY DEPTNO, HIREDATE;

输出:

[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
FOLLWOING ) FROM emp2 ORDER BY DEPTNO, HIREDATE at line 2

最佳答案

有两种方法可以使用 window frame specification :

  • 显式指定下帧边界并隐式使用当前行作为上限
  • 明确指定下限和上限

查看手册:

The frame_extent value indicates the start and end points of the frame. You can specify just the start of the frame (in which case the current row is implicitly the end) or use BETWEEN to specify both frame endpoints

你要写的是这样的:

SELECT 
  deptno, ename, sal, hiredate,
  last_value (hiredate) OVER (
    PARTITION BY deptno 
    ORDER BY hiredate 
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  )
FROM emp2
ORDER BY deptno, hiredate

关于mysql - 为什么 MySQL 中没有无限跟随?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575446/

相关文章:

php - PDO json_encode 中的观看者计数

c# - 是否可以查询实际存储在 Varchar 字段中的 twitter 时间戳?

c# - LINQ 的 SQL 脚本

MySQL 将行转为动态数量的列

sql - Row_Number Sybase SQL Anywhere 在多个条件下更改

sql - 窗口函数字段的别名在 HAVING 和 WHERE 子句中使用时导致 "not found"错误

mysql - 'decimal(10,2)' 附近的存储过程语法错误

mysql - SQL 中的 REPLACE 与 INSERT

sql - 将日期截断为仅小时/分钟

sql - Oracle select 中使用窗口函数的运行总计(累积列)有什么问题?