oracle 分析函数 FIRST_VALUE、LAST_VALUE

用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;

结果:

oracle 分析函数 FIRST_VALUE、LAST_VALUE_第1张图片


对于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;

结果:

oracle 分析函数 FIRST_VALUE、LAST_VALUE_第2张图片


就不对了。


你可能感兴趣的:(oracle 分析函数 FIRST_VALUE、LAST_VALUE)