我有一个配置单元表,其中有列(id,dept,salary),我正在使用spark SQL在该表上进行一些转换。即,如果部门为HR,则薪水=薪水+ 100;如果部门为IT,则薪水=薪水+0,并基于部门收取薪水的总和。[PFB
-表数据
id dept salary
1 HR 100
2 operation 200
3 tech 300
4 IT 400
1 HR 500
-----输出
id dept salary
1 HR 800
2 operation 300
3 tech 300
4 IT 400
我已经编写了以下代码,但在spark sql中不起作用。
**spark.sql("select CASE WHEN dept = 'HR' THEN 'sum(salary+100)',when dept = 'IT' THEN 'sum(salary+0)' ELSE 'salary' END AS salary from emp group by dept").show**
但是输入不匹配:问题。请帮助我应如何实施。
Select Dept, sum(salary + case Dept when 'HR' then 100 when 'IT' then 0 else 0 end)
From emp
Group by Dept
或
Select Dept, sum(salary) + sum(case Dept when 'HR' then 100 when 'IT' then 0 else 0 end)
From emp
Group by Dept