SAMPLE:下面的例子中列prev_sal返回按hire_date排序的前1行的salary值
SELECT last_name, hire_date, salary, LAG(salary, 1, 0) OVER (ORDER BY hire_date) AS prev_sal FROM employees WHERE job_id = 'PU_CLERK';
LAST_NAME HIRE_DATE SALARY PREV_SAL ------------------------- ---------- ---------- ---------- Khoo 18-5月 -95 3100 0 Tobias 24-7月 -97 2800 3100 Baida 24-12月-97 2900 2800 Himuro 15-11月-98 2600 2900 Colmenares 10-8月 -99 2500 2600
LAST
|
功能描述:从DENSE_RANK返回的集合中取出排在最后面的一个值的行(可能多行,因为值可能相等),因此完整的语法需要在开始处加上一个集合函数以从中取出记录
SAMPLE:下面例子中DENSE_RANK按部门分区,再按佣金commission_pct排序,FIRST取出佣金最低的对应的所有行,然后前面的MAX函数从这个集合中取出薪水最低的值;LAST取出佣金最高的对应的所有行,然后前面的MIN函数从这个集合中取出薪水最高的值
SELECT last_name, department_id, salary, MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct) OVER (PARTITION BY department_id) "Worst", MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct) OVER (PARTITION BY department_id) "Best" FROM employees WHERE department_id in (20,80) ORDER BY department_id, salary;
LAST_NAME DEPARTMENT_ID SALARY Worst Best ------------------------- ------------- ---------- ---------- ---------- Fay 20 6000 6000 13000 Hartstein 20 13000 6000 13000 Kumar 80 6100 6100 14000 Banda 80 6200 6100 14000 Johnson 80 6200 6100 14000 Ande 80 6400 6100 14000 Lee 80 6800 6100 14000 Tuvault 80 7000 6100 14000 Sewall 80 7000 6100 14000 Marvins 80 7200 6100 14000 Bates 80 7300 6100 14000 . . .
LAST_VALUE
|
功能描述:返回组中数据窗口的最后一个值。