data
.groupBy("rste")
.agg(collect_list(array("Rax", "RSECT")).as("array"))
.selectExpr(
Array("rste") ++ expressions: _*
)
其中expressions可以创建为:
val nrElements = 6 // or you can aggregate and collect this to be your maximum number, whatever you need
var expressions = Array[String]()
for (i <- 0 until nrElements) {
expressions = expressions :+
s"array[$i][0] as Rax${i + 1}" :+
s"array[$i][1] as RSECT${i + 1}"
}
上述语句将生成以下输出:
Array(array[0][0] as Rax1, array[0][1] as RSECT1, array[1][0] as Rax2, array[1][1] as RSECT2, array[2][0] as Rax3, array[2][1] as RSECT3, array[3][0] as Rax4, array[3][1] as RSECT4, array[4][0] as Rax5, array[4][1] as RSECT5, array[5][0] as Rax6, array[5][1] as RSECT6)
1条答案
按热度按时间bfnvny8b1#
我不确定通过透视的解决方案,但这里有另一个 * 开箱即用 * 的解决方案。
假设
data
包含:我们可以使用
groupBy
、collect_list
,最后使用selectExpr
来提取值,如下所示:其中
expressions
可以创建为:上述语句将生成以下输出:
我们可以将其用作SQL表达式。
完整的输出如下所示:
祝你好运!