ITPub博客

首页 > 数据库 > > Gauss 100 数据库函数介绍之分析函数

Gauss 100 数据库函数介绍之分析函数

作者:GaussDB_数据库 时间:2020-03-02 09:10:08 0 删除 编辑

LAG

语法:

LAG(expr,n ,m) over(partition by expr order by expr)

功能:当前记录前面第n行记录expr列的值。

  • expr是列字段名称。

  • n是大于等于0的整数。

  • m可以为字段值,常量,表达式。

说明:

  • 如果当前记录前面第n行expr列没有值,则返回m。

  • 如果不带参数n,m,则查找当前记录前面一行记录expr列的值,没有则默认值为null。

示例:

返回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.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69959250/viewspace-3134781/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-01-06

  • 博文量
    2907
  • 访问量
    19267