[英]Spark sql group by and sum changing column name?
In this data frame I am finding total salary from each group. 在此数据框中,我正在查找每个组的总工资。 In Oracle I'd use this code 在Oracle中,我将使用此代码
select job_id,sum(salary) as "Total" from hr.employees group by job_id;
In Spark SQL tried the same, I am facing two issues 在Spark SQL中尝试相同,我面临两个问题
empData.groupBy($"job_id").sum("salary").alias("Total").show()
I could not use $
(I think Scala SQL syntax). 我不能使用$
(我认为Scala SQL语法)。 Getting compilation issue 获取编译问题
empData.groupBy($"job_id").sum($"salary").alias("Total").show()
Any idea? 任何想法?
Use Aggregate function .agg()
if you want to provide alias name. 如果要提供别名,请使用聚合函数.agg()
。 This accepts scala syntax ($" ") 这接受scala语法($“”)
empData.groupBy($"job_id").agg(sum($"salary") as "Total").show()
If you dont want to use .agg()
, alias name can be also be provided using .select()
: 如果您不想使用.agg()
,也可以使用.select()
提供别名:
empData.groupBy($"job_id").sum("salary").select($"job_id", $"sum(salary)".alias("Total")).show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.