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

fas

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

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

结果

fa

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 非窗口函数和窗口函数实现以部门分组按薪资排序

结果

faw

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;

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值