LAG
语法:
LAG(expr[,n][,m]) over([ partition by [expr1][ ,... ] ] [order by [expr2][ ,... ] [NULLS FIRST | LAST] ])
功能:返回同一字段的前第N行的数据作为独立的列, 若不存在,则取默认值。
expr1是分组的列字段名称或者表达式,expr2是排序的列字段名称或者表达式。
n是偏移量,大于等于0的整数。
m是默认值,可以为字段值,常量,表达式。
[NULLS FIRST | LAST]请参见•ORDER BY。
说明:
如果当前记录前面第n行expr列没有值,则返回默认值m。
如果不带参数n,m,则返回当前记录前面一行记录expr列的值,没有则默认值为null。
参数n若不设置,则默认值是1。
示例:
返回staffs表中的员工2个月前的工资。
--删除staffs表。
DROP TABLE IF EXISTS staffs;
--创建staffs表。
CREATE TABLE staffs
(
staff_id NUMBER(6) not null,
first_name VARCHAR2(20),
last_name VARCHAR2(25),
email VARCHAR2(25),
phone_number VARCHAR2(20),
hire_date DATE,
employment_id VARCHAR2(10),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6),
section_id NUMBER(4),
graduated_name VARCHAR2(60)
);
--新增数据。
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (198, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK', 2200.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (198, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK', 2400.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (198, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK', 2600.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (199, 'Douglas', 'Grant', 'DGRANT', '650.507.9844', to_date('13-01-2000', 'dd-mm-yyyy'), 'SH_CLERK', 4000.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (199, 'Douglas', 'Grant', 'DGRANT', '650.507.9844', to_date('13-01-2000', 'dd-mm-yyyy'), 'SH_CLERK', 4200.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (199, 'Douglas', 'Grant', 'DGRANT', '650.507.9844', to_date('13-01-2000', 'dd-mm-yyyy'), 'SH_CLERK', 4400.00, null, 124, 50);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (105, 'David', 'Austin', 'DAUSTIN', '590.423.4569', to_date('25-06-1997', 'dd-mm-yyyy'), 'IT_PROG', 4400.00, null, 103, 60);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (105, 'David', 'Austin', 'DAUSTIN', '590.423.4569', to_date('25-06-1997', 'dd-mm-yyyy'), 'IT_PROG', 4600.00, null, 103, 60);
insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date, employment_id, salary, commission_pct, manager_id, section_id)
values (105, 'David', 'Austin', 'DAUSTIN', '590.423.4569', to_date('25-06-1997', 'dd-mm-yyyy'), 'IT_PROG', 4800.00, null, 103, 60);
--返回员工两个月前的工资。
select staff_ID, lag(salary, 2,null)over(partition by staff_ID order by staff_ID) from staffs;
105
105
105 4400
198
198
198 2200
199
199
199 4000
9 rows fetched.
LEAD
语法:
LEAD(expr[,n][,m]) over([ partition by [expr1][ ,... ] ] [order by [expr2][ ,... ] [ ASC | DESC ][NULLS FIRST | LAST] ])
功能:返回同一字段的后第N行的数据作为独立的列, 若不存在,则取默认值。
expr1是分组的列字段名称或者表达式,expr2是排序的列字段名称或者表达式。
n是偏移量,大于等于0的整数。
m是默认值,可以为字段值,常量,表达式。
[NULLS FIRST | LAST]请参见•ORDER BY。
[ ASC | DESC ]请参见•ORDER BY。
说明:
如果当前记录前面第n行expr列没有值,则返回默认值m。
如果不带参数n,m,则返回当前记录前面一行记录expr列的值,没有则默认值为null。
参数n若不设置,则默认值是1。
示例:
返回staffs表中的员工2个月前的工资。
--删除lead_t1表。
DROP TABLE IF EXISTS lead_t1;
--创建lead_t1表。
CREATE TABLE lead_t1
(
orders int ,
levels int,
name varchar(10)
);
--新增数据。
insert into lead_t1 values(1,3,'a1');
insert into lead_t1 values(2,3,'a4');
insert into lead_t1 values(3,3,'a7');
insert into lead_t1 values(4,3,'a10');
insert into lead_t1 values(5,3,'a13');
--返回当前信息和后面第二个的name。
select orders, levels, lead(name,2,-1) over (order by orders asc, levels asc) as "LEAD_ASC" from lead_t1 where orders < 10 order by orders asc, levels desc;
1 3 a7
2 3 a10
3 3 a13
4 3 -1
5 3 -1
5 rows fetched.