为什么在mysql中没有无限的跟随?

时间:2018-07-28 21:35:41

标签: mysql sql window-functions

为什么我不能在mysql中使用无限制的跟随?我可以使用无限制的前置查询,而对同一查询没有任何问题。

select deptno, ENAME, SAL, HIREDATE,
  last_value(HIREDATE) over (partition by deptno order by HIREDATE rows unbounded following )
from emp2
order by DEPTNO, HIREDATE

Output:
[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 'following ) from emp2 order by DEPTNO, HIREDATE' at line 2

1 个答案:

答案 0 :(得分:1)

There are two ways to use the window frame specification:

  • Explicitly specifying the lower frame bound and implicitly using current row as the upper bound
  • Explicitly specifying both lower and upper bounds

See the manual:

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

What you want to write is this:

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