同比和环比函数
本文介绍同比和环比函数的语法规则,包括参数解释、函数示例等。
函数列表
函数 |
描述 |
---|---|
用于对比当前时间周期内的计算结果与n秒之前时间周期内的计算结果。 |
|
用于对比当前时间周期内的计算结果与n秒之前时间周期内的计算结果。 |
compare函数
用于对比当前时间周期内的计算结果与n秒之前时间周期内的计算结果。
语法:compare(x, n1, n2, n3...)
对比当前时间周期内的计算结果与n1、n2、n3秒之前时间周期内的计算结果。
参数名称 |
描述 |
类型 |
是否必选 |
---|---|---|---|
x |
待同比表达式。 |
double |
是 |
n? |
时间窗口,单位为毫秒。例如3600000(1小时)、86400000(1天)、604800000(1周)、31622400000(1年)。 |
long |
是 |
返回值类型:数组。格式为[当前计算结果, n秒前的计算结果, 当前计算结果与n秒前计算结果的比值]。
示例:select compare(c,1) as diff from (select count(type) as c) as t
类型 |
场景 |
---|---|
查询语句 |
compare(c,1) as diff from (select count(type) as c) as t |
返回结果 |
[8,8,1] |
- 8表示当前时间[0,1693533477000]数据总量。
- 8表示前一秒同时间段[1,1693533477001]数据总量。
- 1表示当前时间与前一秒同时间段的数据总量比值。
ts_compare函数
用于对比当前时间周期内的计算结果与n秒之前时间周期内的计算结果。ts_compare函数必须按照时间列进行分组(GROUP BY)。
语法:ts_compare(x, n1, n2, n3...)
对比当前时间周期内的计算结果与n1、n2、n3秒之前时间周期内的计算结果。
参数名称 |
描述 |
类型 |
是否必选 |
---|---|---|---|
x |
待环比表达式。 |
double |
是 |
n? |
时间窗口,单位为毫秒。例如3600000(1小时)、86400000(1天)、604800000(1周)、31622400000(1年)。 |
long |
是 |
返回值类型:数组。格式为[当前计算结果, n秒前的计算结果, 当前计算结果与n秒前计算结果的比值, n秒前的UNIX时间戳]。
示例:select time,ts_compare(pv,86400000) as diff from (select count(*) as pv,date_histogram(field=time, calendar_interval=day) as time group by time) as t group by time
类型 |
场景 |
---|---|
查询语句 |
time,ts_compare(pv,86400000) as diff from (select count(*) as pv,date_histogram(field=time, calendar_interval=day) as time group by time) as t group by time |
返回结果 |
[20,20,1,1698766080000] |