Spark SQL 类似于组内的 LISTAGG() OR GROUP_CONCAT

Ale*_*dro 0 sql apache-spark apache-spark-sql

我需要在组内实现类似于 redshift listagg() 的函数(按 x_column 排序),但在 Spark SQL 中很重要,这里的https://spark.apache.org/docs/2.4.0/api/ sql/

一个类似的问题,但答案不是没有 SQL。

我对 Redshift SQL 的查询是:

select KEY,
listagg(CODE, '-') within group (order by DATE) as CODE
from demo_table
group by KEY
Run Code Online (Sandbox Code Playgroud)

此时,order by 语句并不重要,只需使用 group by 聚合所有列就足够了,我尝试过 concat_ws,但它无法按预期工作

将其放在 pyspark 上对我来说不起作用

钥匙 代码 日期
66 PL 2016年11月1日
66 PL 2016年12月1日
67 吉林 2016年12月1日
67 吉林 2016年10月1日
67 PL 2016年9月1日
67 采购订单 2016年8月1日
67 吉林 2016年12月1日
68 PL 2016年11月1日
68 2016年11月1日

所需输出

钥匙 代码
68 JO-PL
67 JL - JL - PL - PO - JL
68 PL-JO

Mat*_*uff 7

array_joincollect_list

select 
 key, 
 array_join( -- concat the array
  collect_list(code), -- aggregate that collects the array of [code]
  ' - ' -- delimiter 
 )
from demo_table
group by KEY
Run Code Online (Sandbox Code Playgroud)