用SCOTT/TIGER登录。
FIRST_VALUE、LAST_VALUE是两个分析函数。返回结果集中排在第一位和最后一位的值。
使用FIRST_VALUE:
SELECT DEPTNO, JOB, SUM(SAL), FIRST_VALUE(SUM(SAL)) OVER (PARTITION BY DEPTNO ORDER BY SUM(SAL))
FROM EMP GROUP BY DEPTNO, JOB ORDER BY DEPTNO, JOB;
结果:
使用LAST_VALUE:
SELECT DEPTNO, JOB, SUM(SAL), LAST_VALUE(SUM(SAL)) OVER (PARTITION BY DEPTNO ORDER BY SUM(SAL) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM EMP GROUP BY DEPTNO, JOB ORDER BY DEPTNO, JOB;
结果:
对于LAST_VALUE,要加
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
否则,SQL如:
SELECT DEPTNO, JOB, SUM(SAL), LAST_VALUE(SUM(SAL)) OVER (PARTITION BY DEPTNO ORDER BY SUM(SAL))
FROM EMP GROUP BY DEPTNO, JOB ORDER BY DEPTNO, JOB;
结果:
就不对了。