oracle分析函数:三、first,first_value,lag,lead函数
2016-07-08 14:31
393 查看
/************************************************
**分析函数-First/Last函数
**时间:2016-7-8
*************************************************/
--1 语法
aggregate_function keep
( dense_rank first order by expr desc nulls last)
over (query partition clause )
--说明
•DENSE_RANK FIRST or DENSE_RANK LAST indicates that Oracle Database will
aggregate over only those rows with the minimum (FIRST) or the maximum (LAST) dense rank (also called olympic rank).
--**简介:
FIRST and LAST are very similar functions.
Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST
with respect to a given sorting specification.
都聚集和分析功能,给定一个排序的规范,操作一组值从一组行排名第一或最后一个;
If only one row ranks as FIRST or LAST, then the aggregate operates on the set with only one element.
--aggregate functions:
忽略over clause
--analytic functions
指定over clause
-------------------------------------------------
--2 分析例子
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Best"
FROM etl_exdw_log
ORDER BY unit_id, unit_code;
--3 DENSE_RANK FIRST order by expr和dens_rank() over
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Best",
dense_rank() over(partition by unit_id order by out_row),
rank() over(partition by unit_id order by out_row)
FROM etl_exdw_log
ORDER BY unit_id, unit_code;
-------------------------------------------------
--4 聚集函数例子
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) "Best",
dense_rank(23632) within group(order by out_row desc nulls last)
FROM etl_exdw_log
group BY unit_id, unit_code, tjrq_q;
/************************************************
**分析函数-First_value/Last_value函数
**时间:2016-7-8
**简介:
*************************************************/
--1 语法
--ANSI format(推荐):
firt_value (expr (respect|ignore) nulls ) over (analytic clause)
或者:
firt_value (expr) (respect|ignore) nulls over (analytic clause)
-------------------------------------------------
--2 简介
It returns the first value in an ordered set of values;
If the first value in the set is null, then the function returns NULL unless you specify IGNORE NULLS
-------------------------------------------------
--3 例子一
SELECT unit_id,
unit_code,
tjrq_q,
out_row,
FIRST_VALUE(out_row) OVER(partition by unit_id,tjrq_q ORDER BY out_row desc ROWS UNBOUNDED PRECEDING) AS greatest_rows
FROM etl_exdw_log
ORDER BY unit_id,tjrq_q;
/************************************************
**分析函数-lag/lead函数
**时间:2016-7-8
**简介:
*************************************************/
--1 语法1
lag (value_expr,offset,default) (ignore nulls| respect nulls)
over (query_partition_clause order_by_clause)
--2 语法2
lag (value_expr (ignore nulls| respect nulls),offset,default)
over (query_partition_clause order_by_clause)
-------------------------------------------------
--3 目的
LAG provides access to a row at a given physical offset prior to that position.
已知偏移量,根据给的物理便宜量,lag函数可以获得另外一个列
Given a series of rows returned from a query and a position of the cursor
在一个查询结果集中,返回一个游标的位置和一组行
可选的偏移量参数,指定一个整数大于零。如果你不指定偏移量,那么其默认是1。如果偏移量超出窗口范围可选的默认值(1)返回;
{RESPECT | IGNORE} NULLS determines whether null values of value_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.
You cannot nest analytic functions by using LAG or any other analytic function for value_expr.
不能嵌套分析
--lag (value_expr,offset,default) (ignore nulls| respect nulls) over (query_partition_clause order_by_clause)
-------------------------------------------------
--4 例子
SELECT unit_id,
unit_code,
start_time,
end_time,
LAG(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time) AS prev_endtime,
round((end_time - LAG(end_time, 1, sysdate)
OVER(partition by unit_id ORDER BY end_time)) * 24 * 60) as exe_time
FROM ETL_EXDW_LOG
order by unit_id, end_time;
------------------------------------------------
--5 lead函数对比:
SELECT unit_id,
unit_code,
start_time,
end_time,
lead(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time) AS prev_endtime,
round((lead(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time)-end_time) * 24 * 60) as exe_time
FROM ETL_EXDW_LOG
order by unit_id, end_time;
**分析函数-First/Last函数
**时间:2016-7-8
*************************************************/
--1 语法
aggregate_function keep
( dense_rank first order by expr desc nulls last)
over (query partition clause )
--说明
•DENSE_RANK FIRST or DENSE_RANK LAST indicates that Oracle Database will
aggregate over only those rows with the minimum (FIRST) or the maximum (LAST) dense rank (also called olympic rank).
--**简介:
FIRST and LAST are very similar functions.
Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST
with respect to a given sorting specification.
都聚集和分析功能,给定一个排序的规范,操作一组值从一组行排名第一或最后一个;
If only one row ranks as FIRST or LAST, then the aggregate operates on the set with only one element.
--aggregate functions:
忽略over clause
--analytic functions
指定over clause
-------------------------------------------------
--2 分析例子
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Best"
FROM etl_exdw_log
ORDER BY unit_id, unit_code;
--3 DENSE_RANK FIRST order by expr和dens_rank() over
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) OVER(PARTITION BY unit_id) "Best",
dense_rank() over(partition by unit_id order by out_row),
rank() over(partition by unit_id order by out_row)
FROM etl_exdw_log
ORDER BY unit_id, unit_code;
-------------------------------------------------
--4 聚集函数例子
SELECT unit_id,
unit_code,
tjrq_q,
MIN(out_row) KEEP(DENSE_RANK FIRST ORDER BY tjrq_q) "Worst",
MAX(out_row) KEEP(DENSE_RANK LAST ORDER BY tjrq_q) "Best",
dense_rank(23632) within group(order by out_row desc nulls last)
FROM etl_exdw_log
group BY unit_id, unit_code, tjrq_q;
/************************************************
**分析函数-First_value/Last_value函数
**时间:2016-7-8
**简介:
*************************************************/
--1 语法
--ANSI format(推荐):
firt_value (expr (respect|ignore) nulls ) over (analytic clause)
或者:
firt_value (expr) (respect|ignore) nulls over (analytic clause)
-------------------------------------------------
--2 简介
It returns the first value in an ordered set of values;
If the first value in the set is null, then the function returns NULL unless you specify IGNORE NULLS
-------------------------------------------------
--3 例子一
SELECT unit_id,
unit_code,
tjrq_q,
out_row,
FIRST_VALUE(out_row) OVER(partition by unit_id,tjrq_q ORDER BY out_row desc ROWS UNBOUNDED PRECEDING) AS greatest_rows
FROM etl_exdw_log
ORDER BY unit_id,tjrq_q;
/************************************************
**分析函数-lag/lead函数
**时间:2016-7-8
**简介:
*************************************************/
--1 语法1
lag (value_expr,offset,default) (ignore nulls| respect nulls)
over (query_partition_clause order_by_clause)
--2 语法2
lag (value_expr (ignore nulls| respect nulls),offset,default)
over (query_partition_clause order_by_clause)
-------------------------------------------------
--3 目的
LAG provides access to a row at a given physical offset prior to that position.
已知偏移量,根据给的物理便宜量,lag函数可以获得另外一个列
Given a series of rows returned from a query and a position of the cursor
在一个查询结果集中,返回一个游标的位置和一组行
可选的偏移量参数,指定一个整数大于零。如果你不指定偏移量,那么其默认是1。如果偏移量超出窗口范围可选的默认值(1)返回;
{RESPECT | IGNORE} NULLS determines whether null values of value_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.
You cannot nest analytic functions by using LAG or any other analytic function for value_expr.
不能嵌套分析
--lag (value_expr,offset,default) (ignore nulls| respect nulls) over (query_partition_clause order_by_clause)
-------------------------------------------------
--4 例子
SELECT unit_id,
unit_code,
start_time,
end_time,
LAG(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time) AS prev_endtime,
round((end_time - LAG(end_time, 1, sysdate)
OVER(partition by unit_id ORDER BY end_time)) * 24 * 60) as exe_time
FROM ETL_EXDW_LOG
order by unit_id, end_time;
------------------------------------------------
--5 lead函数对比:
SELECT unit_id,
unit_code,
start_time,
end_time,
lead(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time) AS prev_endtime,
round((lead(end_time, 1, sysdate) OVER(partition by unit_id ORDER BY end_time)-end_time) * 24 * 60) as exe_time
FROM ETL_EXDW_LOG
order by unit_id, end_time;
相关文章推荐
- 轻松三步教你配置Oracle—windows环境
- oracle中监听程序当前无法识别连接描述符中请求服务 的解决方法
- oracle 12c:新特性-自动存储管理(ASM)中的增强
- oracle 12c:新特性-同时在多个表上收集统计数据
- oracle 12c:新特性-同时在多个表上收集统计数据
- oracle 12c:新特性-同时在多个表上收集统计数据
- 包失效,无法编译
- linux下ORACLE数据库中创建新用户
- oracle 11g 修改private ip, public ip, vip, scan ip
- oracle sqlloader
- 【OGG】Oracle GoldenGate12c静默安装
- oracle 10g SQL函数
- 用JDBC连接Oracle数据库 via Eclipse
- oracle查询某字段的重复数据
- oracle里实例和数据库之间的关系
- 通过数据库评估存储设备IO性能-Oracle 11g IO校准功能介绍
- Hibenate的JDBC的使用
- Oracle用rowid删除同一张表的重复记录
- Oracle 12c 插件数据库 启动
- PlSql链接不上Oracle问题跟踪