【问题标题】:Spark SQL With Case and Sum带大小写和求和的 Spark SQL
【发布时间】:2020-06-20 05:33:56
【问题描述】:

我有一个配置单元表,其中有列(id、dept、salary)我正在使用 spark SQL 在该表上应用一些转换。

即如果部门是 HR 那么salary=salary+100,如果部门是 IT 那么salary=salary+0 并且基于部门取薪水的总和。光纤通道

输入数据

+----+-----------+--------+
| 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

但输入不匹配:问题。请帮助我应该如何实现这一点。

【问题讨论】:

  • salary+0 对你有意义吗?
  • operation 的输出错误
  • @David דודו Markovitz 不,我需要根据部门增加薪水。(salary+0)只是一个例子。

标签: sql apache-spark hive apache-spark-sql


【解决方案1】:

预期输出:spark sql 查询是

spark.sql("Select id,dept, sum(salary) + sum(case dept when 'HR' then 100 when 'IT' then 0 when 'operation' then 100 else 0 end) as salary  From employee1 group by id,dept order by id asc").show()

我希望这会有所帮助。

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 1970-01-01
      • 2017-04-26
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2010-12-29
      相关资源
      最近更新 更多