sql - 如何用 0 填充 null 并使用 spark `pivot` 累积`count`?
问题描述
我有一些产品销售数据,如下所示:
产品 | 日期 | 钱 |
---|---|---|
一个 | 2020-01 | 60 |
乙 | 2020-03 | 80 |
一个 | 2020-05 | 41 |
乙 | 2020-08 | 50 |
乙 | 2020-12 | 76 |
一个 | 2020-11 | 76 |
我想按以下方式对数据进行分组date
和透视product
我的代码如下
df.groupBy("date").pivot("product").agg(
sum("money").as("month-sum"),
sum(sum("money")).over(Window.orderBy("date").partitionBy("product")).as("cur-cumulative")
).orderBy("date").show()
结果是
| date|A_month-sum|A_cur-cumulative|B_month-sum|B_cur-cumulative|
+-------+-----------+----------------+-----------+----------------+
|2020-01| 60| 60| null| null|
|2020-03| null| null| 80| 140|
|2020-05| 41| 181| null| null|
|2020-08| null| null| 50| 231|
|2020-11| 76| 307| null| null|
|2020-12| null| null| 76| 383|
我的期望是null
ofmonth-sum
可以用 0 填充,null
ofcur-cumulative
可以用最后一行的值填充,就像这样:
| date|A_month-sum|A_cur-cumulative|B_month-sum|B_cur-cumulative|
+-------+-----------+----------------+-----------+----------------+
|2020-01| 60| 60| 0| 0|
|2020-03| 0| 60| 80| 80|
|2020-05| 41| 101| 0| 80|
|2020-08| 0| 101| 50| 130|
|2020-11| 76| 177| 0| 130|
|2020-12| 0| 177| 76| 206|
+-------+-----------+----------------+-----------+----------------+
有什么建议吗?提前致谢!
解决方案
你可以.na.fill(0)
在做累计和之前做:
import org.apache.spark.sql.expressions.Window
val df2 = df
.groupBy("date")
.pivot("product")
.agg(sum("money"))
val df3 = df2
.na.fill(0)
.select(
col("date") +:
df2.columns.tail.flatMap(x =>
Seq(
col(x).as(x + "_month-sum"),
sum(x).over(Window.orderBy("date")).as(x + "_cur-cumulative")
)
): _*
)
.orderBy("date")
df3.show
+-------+-----------+----------------+-----------+----------------+
| date|A_month-sum|A_cur-cumulative|B_month-sum|B_cur-cumulative|
+-------+-----------+----------------+-----------+----------------+
|2020-01| 60.0| 60.0| 0.0| 0.0|
|2020-03| 0.0| 60.0| 80.0| 80.0|
|2020-05| 41.0| 101.0| 0.0| 80.0|
|2020-08| 0.0| 101.0| 50.0| 130.0|
|2020-11| 76.0| 177.0| 0.0| 130.0|
|2020-12| 0.0| 177.0| 76.0| 206.0|
+-------+-----------+----------------+-----------+----------------+
推荐阅读
- django - 无法连接到 amqp://guest:**@127.0.0.1:5672//: [Errno 111] 连接被拒绝
- sql-server - 如何在 MSSQL 中将 YYYY-MM-DD HH:MI:SS 格式转换为 MM-DD-YYYY HH:MI:SS
- ruby - 我怎样才能跳出双'if'块?
- php - 雄辩的关系没有输出
- python - 如何在没有 GUI 的情况下在 Web 上执行操作
- sql-server - 将参数默认值设置为日期/时间
- file-conversion - Firehose 记录格式转换分区
- node.js - 将标头发送到节点js中的客户端后无法设置标头
- mysql - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' in ubuntu for install mysql
- swift - 使用未解析的标识符 swift 3