Hive非窗口函数和窗口函数实现分组排序和个人薪资在部门薪资所占比例

Hive非窗口函数和窗口函数实现分组排序和个人薪资在部门薪资所占比例

表数据(部门编号,员工编号,员工姓名,薪资,。。。)

1. 非窗口函数和窗口函数实现以部门分组计算薪资在整个部门中的比例

结果

Hive非窗口函数和窗口函数实现分组排序和个人薪资在部门薪资所占比例

1.1 非窗口函数实现

with tmp as 
(select deptno,sum(sal) deptno_sal from t_emp
group by deptno
order by deptno)
select e.deptno,e.empno,e.ename,e.sal,t.deptno_sal,round(e.sal/t.deptno_sal,2) zb from t_emp e
inner join tmp t on e.deptno=t.deptno
order by 1,2

1.2 窗口函数实现

Select deptno,empno,ename,sal,sum(sal) over(partition by deptno) sumSal,round(sal/sum(sal) over(partition by deptno),2) rate from t_emp order by 1,2

2 非窗口函数和窗口函数实现以部门分组按薪资排序

结果

Hive非窗口函数和窗口函数实现分组排序和个人薪资在部门薪资所占比例

2.1 非窗口函数实现(表自身inner join)

select deptno,a_sal,count(1) from
(
select a.deptno,a.empno,a.sal a_sal,b.sal b_sal from t_emp a
inner join t_emp b  
on a.deptno=b.deptno and a.sal<=b.sal
order by a.deptno,a_sal
) tmp
group by deptno,a_sal
order by 1,3 

2.2 窗口函数实现

select deptno,empno,ename,sal,row_number() over(partition by deptno order by sal desc) rn from t_emp;