- 最新动态
- 功能总览
- 服务公告
- 产品介绍
- 计费说明
- 快速入门
-
用户指南
- DLI作业开发流程
- 准备工作
- 创建弹性资源池和队列
- 创建数据库和表
- 数据迁移与数据传输
- 配置DLI访问其他云服务的委托权限
- 使用DLI提交SQL作业
- 使用DLI提交Flink作业
- 使用DLI提交Spark作业
- 使用Notebook实例提交DLI作业
- 使用CES监控DLI服务
- 使用CTS审计DLI服务
- 权限管理
- DLI常用管理操作
- 最佳实践
-
开发指南
- 使用客户端工具连接DLI
- SQL作业开发指南
- Flink作业开发指南
- Spark Jar作业开发指南
-
Spark SQL语法参考
- Spark SQL常用配置项说明
- Spark SQL语法概览
- Spark开源命令支持说明
- 数据库相关
- 表相关
- 数据相关
- 导出查询结果
- 跨源连接相关
- 视图相关
- 查看计划
- 数据权限相关
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- class_name
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- hdfs_path
- input_expression
- input_format_classname
- jar_path
- join_condition
- non_equi_join_condition
- number
- num_buckets
- output_format_classname
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- row_format
- select_statement
- separator
- serde_name
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- view_name
- view_properties
- when_expression
- where_condition
- window_function
- 运算符
-
Flink SQL语法参考
- Flink Opensource SQL1.15语法参考
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
-
HetuEngine SQL语法参考
-
HetuEngine SQL语法
- 使用前必读
- 数据类型
-
DDL 语法
- CREATE SCHEMA
- CREATE TABLE
- CREATE TABLE AS
- CREATE TABLE LIKE
- CREATE VIEW
- ALTER TABLE
- ALTER VIEW
- ALTER SCHEMA
- DROP SCHEMA
- DROP TABLE
- DROP VIEW
- TRUNCATE TABLE
- COMMENT
- VALUES
- SHOW语法使用概要
- SHOW SCHEMAS(DATABASES)
- SHOW TABLES
- SHOW TBLPROPERTIES TABLE|VIEW
- SHOW TABLE/PARTITION EXTENDED
- SHOW FUNCTIONS
- SHOW PARTITIONS
- SHOW COLUMNS
- SHOW CREATE TABLE
- SHOW VIEWS
- SHOW CREATE VIEW
- DML 语法
- DQL 语法
- 辅助命令语法
- 预留关键字
- SQL函数和操作符
- 数据类型隐式转换
- 附录
-
HetuEngine SQL语法
- Hudi SQL语法参考
- Delta SQL语法参考
-
API参考
- API使用前必读
- API概览
- 如何调用API
- API快速入门
- 权限相关API
- 全局变量相关API
- 资源标签相关API
- 增强型跨源连接相关API
- 跨源认证相关API
- 弹性资源池相关API
- 队列相关API(推荐)
- SQL作业相关API
- SQL模板相关API
- Flink作业相关API
- Flink作业模板相关API
- Flink作业管理相关API
- Spark作业相关API
- Spark作业模板相关API
- 权限策略和授权项
- 历史API
- 公共参数
- SDK参考
-
常见问题
- DLI产品咨询类
- DLI弹性资源池和队列类
-
DLI数据库和表类
- 为什么在DLI控制台中查询不到表?
- OBS表压缩率较高怎么办?
- 字符码不一致导致数据乱码怎么办?
- 删除表后再重新创建同名的表,需要对操作该表的用户和项目重新赋权吗?
- DLI分区内表导入的文件不包含分区列的数据,导致数据导入完成后查询表数据失败怎么办?
- 创建OBS外表,由于OBS文件中的某字段存在换行符导致表字段数据错误怎么办?
- join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败怎么办?
- 手动在OBS表的分区目录下添加了数据,但是无法查询到数据怎么办?
- 为什么insert overwrite覆盖分区表数据的时候,覆盖了全量数据?
- 跨源连接RDS表中create_date字段类型是datetime,为什么DLI中查出来的是时间戳呢?
- SQL作业执行完成后,修改表名导致datasize不正确怎么办?
- 从DLI导入数据到OBS,数据量不一致怎么办?
-
增强型跨源连接类
- 增强型跨源连接绑定队列失败怎么办?
- DLI增强型跨源连接DWS失败怎么办?
- 创建跨源成功但测试网络连通性失败怎么办?
- 怎样配置DLI队列与数据源的网络连通?
- 为什么DLI增强型跨源连接要创建对等连接?
- DLI创建跨源连接,绑定队列一直在创建中怎么办?
- 新建跨源连接,显示已激活,但使用时提示communication link failure错误怎么办?
- 跨源访问MRS HBase,连接超时,日志未打印错误怎么办?
- DLI跨源连接报错找不到子网怎么办?
- 跨源RDS表,执行insert overwrite提示Incorrect string value错误怎么办?
- 创建RDS跨源表提示空指针错误怎么办?
- 对跨源DWS表执行insert overwrite操作,报错:org.postgresql.util.PSQLException: ERROR: tuple concurrently updated
- 通过跨源表向CloudTable Hbase表导入数据,executor报错:RegionTooBusyException
- 通过DLI跨源写DWS表,非空字段出现空值异常怎么办?
- 更新跨源目的端源表后,未同时更新对应跨源表,导致insert作业失败怎么办?
- RDS表有自增主键时怎样在DLI插入数据?
-
SQL作业类
- SQL作业开发类
-
SQL作业运维类
- 用户导表到OBS报“path obs://xxx already exists”错误
- 对两个表进行join操作时,提示:SQL_ANALYSIS_ERROR: Reference 't.id' is ambiguous, could be: t.id, t.id.;
- 执行查询语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
- 执行查询语句报错:There should be at least one partition pruning predicate on partitioned table XX.YYY
- LOAD数据到OBS外表报错:IllegalArgumentException: Buffer size too small. size
- SQL作业运行报错:DLI.0002 FileNotFoundException
- 用户通过CTAS创建hive表报schema解析异常错误
- 在DataArts Studio上运行DLI SQL脚本,执行结果报org.apache.hadoop.fs.obs.OBSIOException错误
- 使用CDM迁移数据到DLI,迁移作业日志上报UQUERY_CONNECTOR_0001:Invoke DLI service api failed错误
- SQL作业访问报错:File not Found
- SQL作业访问报错:DLI.0003: AccessControlException XXX
- SQL作业访问外表报错:DLI.0001: org.apache.hadoop.security.AccessControlException: verifyBucketExists on {{桶名}}: status [403]
- 执行SQL语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
-
Flink作业类
- Flink作业咨询类
-
Flink SQL作业类
- 怎样将OBS表映射为DLI的分区表?
- Flink SQL作业Kafka分区数增加或减少,怎样不停止Flink作业实现动态感知?
- 在Flink SQL作业中创建表使用EL表达式,作业运行提示DLI.0005错误怎么办?
- Flink作业输出流写入数据到OBS,通过该OBS文件路径创建的DLI表查询无数据
- Flink SQL作业运行失败,日志中有connect to DIS failed java.lang.IllegalArgumentException: Access key cannot be null错误
- Flink SQL作业消费Kafka后sink到es集群,作业执行成功,但未写入数据
- Flink Opensource SQL如何解析复杂嵌套 JSON?
- Flink Opensource SQL从RDS数据库读取的时间和RDS数据库存储的时间为什么会不一致?
- Flink Opensource SQL Elasticsearch结果表failure-handler参数填写retry_rejected导致提交失败
- Kafka Sink配置发送失败重试机制
- 如何在一个Flink作业中将数据写入到不同的Elasticsearch集群中?
- 作业语义检验时提示DIS通道不存在怎么处理?
- Flink jobmanager日志一直报Timeout expired while fetching topic metadata怎么办?
- Flink Jar作业类
- Flink作业性能调优类
-
Spark作业相类
- Spark作业开发类
-
Spark作业运维类
- 运行Spark作业报java.lang.AbstractMethodError
- Spark作业访问OBS数据时报ResponseCode: 403和ResponseStatus: Forbidden错误
- 有访问OBS对应的桶的权限,但是Spark作业访问时报错 verifyBucketExists on XXXX: status [403]
- Spark作业运行大批量数据时上报作业运行超时异常错误
- 使用Spark作业访问sftp中的文件,作业运行失败,日志显示访问目录异常
- 执行作业的用户数据库和表权限不足导致作业运行失败
- 为什么Spark3.x的作业日志中打印找不到global_temp数据库
- 在使用Spark2.3.x访问元数据时,DataSource语法创建avro类型的OBS表创建失败
- DLI资源配额类
- DLI权限管理类
- DLI API类
- 视频帮助
-
更多文档
- 用户指南(阿布扎比区域)
- API参考(阿布扎比区域)
-
SQL语法参考(阿布扎比区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(巴黎区域)
- API参考 (巴黎区域)
-
SQL语法参考(巴黎区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(吉隆坡区域)
- API参考(吉隆坡区域)
-
SQL语法参考(吉隆坡区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 多版本备份恢复数据
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 通用参考
链接复制成功!
窗口函数
窗口函数跨查询结果的行执行计算。它们在HAVING子句之后但在ORDER BY子句之前运行。调用窗口函数需要使用OVER子句来指定窗口的特殊语法。窗口具有三个组成部分:
- 分区规范,它将输入行分为不同的分区。这类似于GROUP BY子句如何将行分为聚合函数的不同组。
- 排序规范,它确定窗口函数将处理输入行的顺序。
- 窗口框架,指定给定行该功能要处理的行的滑动窗口。如果未指定帧,则默认为“RANGE UNBOUNDED PRECEDING”,与“UNBOUNDEEN PREBODING AND CURRENT ROWGE”相同。该帧包含从分区的开始到当前行的最后一个对等方的所有行。在没有ORDER BY的情况下,所有行都被视为对等行,因此未绑定的前导和当前行之间的范围等于未绑定的前导和未绑定的后续之间的范围。
例如:下面的查询将salary表中的信息按照每个部门员工工资的大小进行排序。
--创建数据表并插入数据 create table salary (dept varchar, userid varchar, sal double); insert into salary values ('d1','user1',1000),('d1','user2',2000),('d1','user3',3000),('d2','user4',4000),('d2','user5',5000); --数据查询 select dept,userid,sal,rank() over (partition by dept order by sal desc) as rnk from salary order by dept,rnk; dept | userid | sal | rnk ------|--------|--------|----- d1 | user3 | 3000.0 | 1 d1 | user2 | 2000.0 | 2 d1 | user1 | 1000.0 | 3 d2 | user5 | 5000.0 | 1 d2 | user4 | 4000.0 | 2
Aggregate Functions
所有的聚合函数都能通过添加over子句来当做窗口函数使用。聚合函数将在当前窗口框架下的每行记录进行运算。
下面的查询生成每个职员按天计算的订单价格的滚动总和。
select dept,userid,sal,sum(sal) over (partition by dept order by sal desc) as rolling_sum from salary order by dept,userid,sal; dept | userid | sal | rolling_sum ------|--------|--------|------------- d1 | user1 | 1000.0 | 6000.0 d1 | user2 | 2000.0 | 5000.0 d1 | user3 | 3000.0 | 3000.0 d2 | user4 | 4000.0 | 9000.0 d2 | user5 | 5000.0 | 5000.0 (5 rows)
Ranking Functions
- cume_dist()→ bigint
描述:小于等于当前值的行数/分组内总行数–比如,统计小于等于当前薪水的人数,所占总人数的比例。
--查询示例 SELECT dept, userid, sal, CUME_DIST() OVER(ORDER BY sal) AS rn1, CUME_DIST() OVER(PARTITION BY dept ORDER BY sal) AS rn2 FROM salary; dept | userid | sal | rn1 | rn2 ------|--------|--------|-----|-------------------- d2 | user4 | 4000.0 | 0.8 | 0.5 d2 | user5 | 5000.0 | 1.0 | 1.0 d1 | user1 | 1000.0 | 0.2 | 0.3333333333333333 d1 | user2 | 2000.0 | 0.4 | 0.6666666666666666 d1 | user3 | 3000.0 | 0.6 | 1.0 (5 rows)
- dense_rank()→ bigint
- ntile(n)→ bigint
描述:用于将分组数据按照顺序切分成n片,返回当前切片值。NTILE不支持ROWS BETWEEN,比如NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)如果切片不均匀,默认增加第一个切片的分布。
--创建表并插入数据 create table cookies_log (cookieid varchar,createtime date,pv int); insert into cookies_log values ('cookie1',date '2020-07-10',1), ('cookie1',date '2020-07-11',5), ('cookie1',date '2020-07-12',7), ('cookie1',date '2020-07-13',3), ('cookie1',date '2020-07-14',2), ('cookie1',date '2020-07-15',4), ('cookie1',date '2020-07-16',4), ('cookie2',date '2020-07-10',2), ('cookie2',date '2020-07-11',3), ('cookie2',date '2020-07-12',5), ('cookie2',date '2020-07-13',6), ('cookie2',date '2020-07-14',3), ('cookie2',date '2020-07-15',9), ('cookie2',date '2020-07-16',7); -- 查询结果 SELECT cookieid,createtime,pv, NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn1, --分组内将数据分成2片 NTILE(3) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn2, --分组内将数据分成3片 NTILE(4) OVER(ORDER BY createtime) AS rn3 --将所有数据分成4片 FROM cookies_log ORDER BY cookieid,createtime; cookieid | createtime | pv | rn1 | rn2 | rn3 ----------|------------|----|-----|-----|----- cookie1 | 2020-07-10 | 1 | 1 | 1 | 1 cookie1 | 2020-07-11 | 5 | 1 | 1 | 1 cookie1 | 2020-07-12 | 7 | 1 | 1 | 2 cookie1 | 2020-07-13 | 3 | 1 | 2 | 2 cookie1 | 2020-07-14 | 2 | 2 | 2 | 3 cookie1 | 2020-07-15 | 4 | 2 | 3 | 4 cookie1 | 2020-07-16 | 4 | 2 | 3 | 4 cookie2 | 2020-07-10 | 2 | 1 | 1 | 1 cookie2 | 2020-07-11 | 3 | 1 | 1 | 1 cookie2 | 2020-07-12 | 5 | 1 | 1 | 2 cookie2 | 2020-07-13 | 6 | 1 | 2 | 2 cookie2 | 2020-07-14 | 3 | 2 | 2 | 3 cookie2 | 2020-07-15 | 9 | 2 | 3 | 3 cookie2 | 2020-07-16 | 7 | 2 | 3 | 4 (14 rows)
- percent_rank()→ double
描述:返回值在一组值中的百分比排名。 结果为(r-1)/(n-1),其中r是该行的rank(),n是窗口分区中的总行数。
SELECT dept,userid,sal, PERCENT_RANK() OVER(ORDER BY sal) AS rn1, --分组内 RANK() OVER(ORDER BY sal) AS rn11, --分组内RANK值 SUM(1) OVER(PARTITION BY NULL) AS rn12, --分组内总行数 PERCENT_RANK() OVER(PARTITION BY dept ORDER BY sal) AS rn2 from salary; dept | userid | sal | rn1 | rn11 | rn12 | rn2 ------|--------|--------|------|------|------|----- d2 | user4 | 4000.0 | 0.75 | 4 | 5 | 0.0 d2 | user5 | 5000.0 | 1.0 | 5 | 5 | 1.0 d1 | user1 | 1000.0 | 0.0 | 1 | 5 | 0.0 d1 | user2 | 2000.0 | 0.25 | 2 | 5 | 0.5 d1 | user3 | 3000.0 | 0.5 | 3 | 5 | 1.0 (5 rows)
- rank()→ bigint
描述:返回值在一组值中的排名。等级为1加上该行之前与该行不对等的行数。因此,排序中的平局值将在序列中产生缺口。对每个窗口分区执行排名。
SELECT cookieid, createtime, pv, RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn1, DENSE_RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn2, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn3 FROM cookies_log WHERE cookieid = 'cookie1'; cookieid | createtime | pv | rn1 | rn2 | rn3 ----------|------------|----|-----|-----|----- cookie1 | 2020-07-12 | 7 | 1 | 1 | 1 cookie1 | 2020-07-11 | 5 | 2 | 2 | 2 cookie1 | 2020-07-15 | 4 | 3 | 3 | 3 cookie1 | 2020-07-16 | 4 | 3 | 3 | 4 cookie1 | 2020-07-13 | 3 | 5 | 4 | 5 cookie1 | 2020-07-14 | 2 | 6 | 5 | 6 cookie1 | 2020-07-10 | 1 | 7 | 6 | 7 (7 rows)
- row_number()→ bigint
描述:从1开始,按照顺序,生成分组内记录的序列–比如,按照pv降序排列,生成分组内每天的pv名次ROW_NUMBER() 的应用场景非常多,再比如,获取分组内排序第一的记录。获取一个session中的第一条refer等。
SELECT cookieid, createtime, pv, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn from cookies_log; cookieid | createtime | pv | rn ----------|------------- |----|---- cookie2 | 2020-07-15 | 9 | 1 cookie2 | 2020-07-16 | 7 | 2 cookie2 | 2020-07-13 | 6 | 3 cookie2 | 2020-07-12 | 5 | 4 cookie2 | 2020-07-14 | 3 | 5 cookie2 | 2020-07-11 | 3 | 6 cookie2 | 2020-07-10 | 2 | 7 cookie1 | 2020-07-12 | 7 | 1 cookie1 | 2020-07-11 | 5 | 2 cookie1 | 2020-07-15 | 4 | 3 cookie1 | 2020-07-16 | 4 | 4 cookie1 | 2020-07-13 | 3 | 5 cookie1 | 2020-07-14 | 2 | 6 cookie1 | 2020-07-10 | 1 | 7 (14 rows)
Value Functions
通常情况下,要重视null值。如果指定了IGNORE NULLS,那么计算中所有包含x为null值的行都会被排除掉,如果所有行的x字段值都是null值,将会返回默认值,否则返回null值。
-- 数据准备 create table cookie_views( cookieid varchar,createtime timestamp,url varchar); insert into cookie_views values ('cookie1',timestamp '2020-07-10 10:00:02','url20'), ('cookie1',timestamp '2020-07-10 10:00:00','url10'), ('cookie1',timestamp '2020-07-10 10:03:04','urll3'), ('cookie1',timestamp '2020-07-10 10:50:05','url60'), ('cookie1',timestamp '2020-07-10 11:00:00','url70'), ('cookie1',timestamp '2020-07-10 10:10:00','url40'), ('cookie1',timestamp '2020-07-10 10:50:01','url50'), ('cookie2',timestamp '2020-07-10 10:00:02','url23'), ('cookie2',timestamp '2020-07-10 10:00:00','url11'), ('cookie2',timestamp '2020-07-10 10:03:04','url33'), ('cookie2',timestamp '2020-07-10 10:50:05','url66'), ('cookie2',timestamp '2020-07-10 11:00:00','url77'), ('cookie2',timestamp '2020-07-10 10:10:00','url47'), ('cookie2',timestamp '2020-07-10 10:50:01','url55');
- first_value(x)→ [same as input]
描述:返回窗口的第一个值。
SELECT cookieid, createtime, url, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY createtime) AS rn, FIRST_VALUE(url) OVER(PARTITION BY cookieid ORDER BY createtime) AS first1 FROM cookie_views; cookieid | createtime | url | rn | first1 ----------|-------------------------|-------|----|-------- cookie1 | 2020-07-10 10:00:00.000 | url10 | 1 | url10 cookie1 | 2020-07-10 10:00:02.000 | url20 | 2 | url10 cookie1 | 2020-07-10 10:03:04.000 | urll3 | 3 | url10 cookie1 | 2020-07-10 10:10:00.000 | url40 | 4 | url10 cookie1 | 2020-07-10 10:50:01.000 | url50 | 5 | url10 cookie1 | 2020-07-10 10:50:05.000 | url60 | 6 | url10 cookie1 | 2020-07-10 11:00:00.000 | url70 | 7 | url10 cookie2 | 2020-07-10 10:00:00.000 | url11 | 1 | url11 cookie2 | 2020-07-10 10:00:02.000 | url23 | 2 | url11 cookie2 | 2020-07-10 10:03:04.000 | url33 | 3 | url11 cookie2 | 2020-07-10 10:10:00.000 | url47 | 4 | url11 cookie2 | 2020-07-10 10:50:01.000 | url55 | 5 | url11 cookie2 | 2020-07-10 10:50:05.000 | url66 | 6 | url11 cookie2 | 2020-07-10 11:00:00.000 | url77 | 7 | url11 (14 rows)
- last_value(x)→ [same as input]
描述:返回窗口的最后一个值。
SELECT cookieid,createtime,url, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY createtime) AS rn, LAST_VALUE(url) OVER(PARTITION BY cookieid ORDER BY createtime) AS last1 FROM cookie_views; cookieid | createtime | url | rn | last1 ----------|-------------------------|-------|----|------- cookie2 | 2020-07-10 10:00:00.000 | url11 | 1 | url11 cookie2 | 2020-07-10 10:00:02.000 | url23 | 2 | url23 cookie2 | 2020-07-10 10:03:04.000 | url33 | 3 | url33 cookie2 | 2020-07-10 10:10:00.000 | url47 | 4 | url47 cookie2 | 2020-07-10 10:50:01.000 | url55 | 5 | url55 cookie2 | 2020-07-10 10:50:05.000 | url66 | 6 | url66 cookie2 | 2020-07-10 11:00:00.000 | url77 | 7 | url77 cookie1 | 2020-07-10 10:00:00.000 | url10 | 1 | url10 cookie1 | 2020-07-10 10:00:02.000 | url20 | 2 | url20 cookie1 | 2020-07-10 10:03:04.000 | urll3 | 3 | urll3 cookie1 | 2020-07-10 10:10:00.000 | url40 | 4 | url40 cookie1 | 2020-07-10 10:50:01.000 | url50 | 5 | url50 cookie1 | 2020-07-10 10:50:05.000 | url60 | 6 | url60 cookie1 | 2020-07-10 11:00:00.000 | url70 | 7 | url70 (14 rows)
- nth_value(x, offset)→ [same as input]
描述:返回距窗口开头指定偏移量的值。偏移量从1开始。偏移量可以是任何标量表达式。如果偏移量为null或大于窗口中的值数,则返回null。偏移量不允许为0或者负数。
SELECT cookieid,createtime,url, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY createtime) AS rn, NTH_VALUE(url,3) OVER(PARTITION BY cookieid ORDER BY createtime) AS last1 FROM cookie_views; cookieid | createtime | url | rn | last1 ----------|-------------------------|-------|----|------- cookie1 | 2020-07-10 10:00:00.000 | url10 | 1 | NULL cookie1 | 2020-07-10 10:00:02.000 | url20 | 2 | NULL cookie1 | 2020-07-10 10:03:04.000 | urll3 | 3 | urll3 cookie1 | 2020-07-10 10:10:00.000 | url40 | 4 | urll3 cookie1 | 2020-07-10 10:50:01.000 | url50 | 5 | urll3 cookie1 | 2020-07-10 10:50:05.000 | url60 | 6 | urll3 cookie1 | 2020-07-10 11:00:00.000 | url70 | 7 | urll3 cookie2 | 2020-07-10 10:00:00.000 | url11 | 1 | NULL cookie2 | 2020-07-10 10:00:02.000 | url23 | 2 | NULL cookie2 | 2020-07-10 10:03:04.000 | url33 | 3 | url33 cookie2 | 2020-07-10 10:10:00.000 | url47 | 4 | url33 cookie2 | 2020-07-10 10:50:01.000 | url55 | 5 | url33 cookie2 | 2020-07-10 10:50:05.000 | url66 | 6 | url33 cookie2 | 2020-07-10 11:00:00.000 | url77 | 7 | url33 (14 rows)
- lead(x[, offset[, default_value]])→ [same as input]
描述:返回窗口分区中当前行之后的偏移行处的值。偏移量从0开始,即当前行。偏移量可以是任何标量表达式。默认偏移量为1。如果偏移量为null,则返回null。如果偏移量指向不在分区内的行,则返回default_value,或者如果未指定,则返回null。lead()函数要求指定窗口顺序。不得指定窗框。
SELECT cookieid,createtime,url, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY createtime) AS rn, LEAD(createtime,1,timestamp '2020-01-01 00:00:00') OVER(PARTITION BY cookieid ORDER BY createtime) AS next_1_time, LEAD(createtime,2) OVER(PARTITION BY cookieid ORDER BY createtime) AS next_2_time FROM cookie_views; cookieid | createtime | url | rn | next_1_time | next_2_time ----------|-------------------------|-------|----|-------------------------|------------------------- cookie2 | 2020-07-10 10:00:00.000 | url11 | 1 | 2020-07-10 10:00:02.000 | 2020-07-10 10:03:04.000 cookie2 | 2020-07-10 10:00:02.000 | url23 | 2 | 2020-07-10 10:03:04.000 | 2020-07-10 10:10:00.000 cookie2 | 2020-07-10 10:03:04.000 | url33 | 3 | 2020-07-10 10:10:00.000 | 2020-07-10 10:50:01.000 cookie2 | 2020-07-10 10:10:00.000 | url47 | 4 | 2020-07-10 10:50:01.000 | 2020-07-10 10:50:05.000 cookie2 | 2020-07-10 10:50:01.000 | url55 | 5 | 2020-07-10 10:50:05.000 | 2020-07-10 11:00:00.000 cookie2 | 2020-07-10 10:50:05.000 | url66 | 6 | 2020-07-10 11:00:00.000 | NULL cookie2 | 2020-07-10 11:00:00.000 | url77 | 7 | 2020-01-01 00:00:00.000 | NULL cookie1 | 2020-07-10 10:00:00.000 | url10 | 1 | 2020-07-10 10:00:02.000 | 2020-07-10 10:03:04.000 cookie1 | 2020-07-10 10:00:02.000 | url20 | 2 | 2020-07-10 10:03:04.000 | 2020-07-10 10:10:00.000 cookie1 | 2020-07-10 10:03:04.000 | urll3 | 3 | 2020-07-10 10:10:00.000 | 2020-07-10 10:50:01.000 cookie1 | 2020-07-10 10:10:00.000 | url40 | 4 | 2020-07-10 10:50:01.000 | 2020-07-10 10:50:05.000 cookie1 | 2020-07-10 10:50:01.000 | url50 | 5 | 2020-07-10 10:50:05.000 | 2020-07-10 11:00:00.000 cookie1 | 2020-07-10 10:50:05.000 | url60 | 6 | 2020-07-10 11:00:00.000 | NULL cookie1 | 2020-07-10 11:00:00.000 | url70 | 7 | 2020-01-01 00:00:00.000 | NULL (14 rows)
- lag(x[, offset[, default_value]])→ [same as input]
描述:返回窗口分区中当前行之前的偏移行的值,偏移量从0开始,即当前行,偏移量可以是任何标量表达式,默认偏移量为1。如果偏移量为null,则返回null。如果偏移量指向不在分区内的行,则返回default_value。如果未指定,则返回null。lag()函数要求指定窗口顺序,不得指定窗框。
SELECT cookieid, createtime, url, ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY createtime) AS rn, LAG(createtime,1, timestamp '2020-01-01 00:00:00') OVER(PARTITION BY cookieid ORDER BY createtime) AS last_1_time, LAG(createtime,2) OVER(PARTITION BY cookieid ORDER BY createtime) AS last_2_time FROM cookie_views; cookieid | createtime | url | rn | last_1_time | last_2_time ----------|-------------------------|-------|----|-------------------------|----------------------- cookie2 | 2020-07-10 10:00:00.000 | url11 | 1 | 2020-01-01 00:00:00.000 | NULL cookie2 | 2020-07-10 10:00:02.000 | url23 | 2 | 2020-07-10 10:00:00.000 | NULL cookie2 | 2020-07-10 10:03:04.000 | url33 | 3 | 2020-07-10 10:00:02.000 | 2020-07-10 10:00:00.000 cookie2 | 2020-07-10 10:10:00.000 | url47 | 4 | 2020-07-10 10:03:04.000 | 2020-07-10 10:00:02.000 cookie2 | 2020-07-10 10:50:01.000 | url55 | 5 | 2020-07-10 10:10:00.000 | 2020-07-10 10:03:04.000 cookie2 | 2020-07-10 10:50:05.000 | url66 | 6 | 2020-07-10 10:50:01.000 | 2020-07-10 10:10:00.000 cookie2 | 2020-07-10 11:00:00.000 | url77 | 7 | 2020-07-10 10:50:05.000 | 2020-07-10 10:50:01.000 cookie1 | 2020-07-10 10:00:00.000 | url10 | 1 | 2020-01-01 00:00:00.000 | NULL cookie1 | 2020-07-10 10:00:02.000 | url20 | 2 | 2020-07-10 10:00:00.000 | NULL cookie1 | 2020-07-10 10:03:04.000 | urll3 | 3 | 2020-07-10 10:00:02.000 | 2020-07-10 10:00:00.000 cookie1 | 2020-07-10 10:10:00.000 | url40 | 4 | 2020-07-10 10:03:04.000 | 2020-07-10 10:00:02.000 cookie1 | 2020-07-10 10:50:01.000 | url50 | 5 | 2020-07-10 10:10:00.000 | 2020-07-10 10:03:04.000 cookie1 | 2020-07-10 10:50:05.000 | url60 | 6 | 2020-07-10 10:50:01.000 | 2020-07-10 10:10:00.000 cookie1 | 2020-07-10 11:00:00.000 | url70 | 7 | 2020-07-10 10:50:05.000 | 2020-07-10 10:50:01.000 (14 rows)