一般在hive中求同比环比都需要表自关联,其实还有一种更优雅的办法。
hive中有个lag函数,正好可以用于求同比环比,不过要求数据比较完整
LAG(col,n,DEFAULT) 用于统计窗口内往上第n行值
第一个参数为列名,
第二个参数为往上第n行(可选,默认为1),
第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)
num1即为上个月的值,num2即为12个月之前的值
select year_id,month_id,num, lag(num,1,0) over (order by year_id,month_id) num1, lag(num,12,0) over (order by year_id,month_id) num2, num/(lag(num,1,0) over (order by year_id,month_id))-1 as num3, num/(lag(num,12,0) over (order by year_id,month_id))-1 as num4 from (select year_id, month_id, count(distinct prem_id) as num from cisadm_dwd.dwd_cis_wo_repair_di group by year_id,month_id order by year_id,month_id)a