Spark Sql合并多行内容为一行

  • Post author:
  • Post category:其他




用到的函数及定义


concat_ws(sep, [str | array(str)]+)

– Returns the concatenation of the strings separated by sep.


Examples:

SELECT concat_ws(’ ‘, ‘Spark’, ‘SQL’);

Spark SQL


collect_set(expr)

– Collects and returns a set of unique elements.



数据用例

在这里插入图片描述

我们要把第四列的字符串合并为一行

select 
	Asset_a
	, concat_ws(',',collect_set(NT_Login)) as ntlogin 
from (
	select 
	Asset
	,Platform
	,UserOrBatch
	,NT_Login
	from eip_rewards_usage  
	where Platform='hercules' 
	and UserOrBatch='User'
	)
group by 1
order by 1 asc

先对我们所需的数据增加限制进行group by,然后组内NT_Login数据项collect_set在一起,concat_ws进行合并,’,’作为分隔符

sparksql函数文档:

https://docs.databricks.com/spark/latest/spark-sql/language-manual/functions.html#concat_ws



版权声明:本文为DUTwangtaiyu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。