这是我的例子:
SELECT empno,
ename,
job,
sal,
LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
sal - LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_diff
FROM emp;
在这里输出:
EMPNO| ENAME| JOB | SAL | SAL_PREV | SAL_DIFF
-----|------|-----------------------------------------------
1000| SMITH| CLERK | 800 | 0 | 800
1001| JAMES| CLERK | 950 | 800 | 150
1002| ADAMS| CLERK | 1100 | 950 | 150
1003| WARD | SALESMAN | 1250 | 1100 | 150
我知道延迟仅适用于与where子句过滤器匹配的行,但是如果我添加where EMPNO > 1001
我无法获取之前的值,是否有正确的方法来检索未包含的先前值关闭数据?
我使用的是Oracle 12c。
答案 0 :(得分:0)
使用子查询:
SELECT e.*
FROM (SELECT empno, ename, job, sal,
LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
(sal - LAG(sal, 1, 0) OVER (empno ORDER BY sal)) AS sal_diff
FROM emp
) e
WHERE empno > 1001;