【发布时间】:2023-04-08 10:06:01
【问题描述】:
我在 AWS Glue 中使用 python3.6 的环境 pyspark 中工作。我有这张桌子:
+----+-----+-----+-----+
|year|month|total| loop|
+----+-----+-----+-----+
|2012| 1| 20|loop1|
|2012| 2| 30|loop1|
|2012| 1| 10|loop2|
|2012| 2| 5|loop2|
|2012| 1| 50|loop3|
|2012| 2| 60|loop3|
+----+-----+-----+-----+
我需要得到如下输出:
year month total_loop1 total_loop2 total_loop3
2012 1 20 10 50
2012 2 30 5 60
我得到的更接近的是 SQL 代码:
select a.year,a.month, a.total,b.total from test a
left join test b
on a.loop <> b.loop
and a.year = b.year and a.month=b.month
到目前为止仍然有输出:
+----+-----+-----+-----+
|year|month|total|total|
+----+-----+-----+-----+
|2012| 1| 20| 10|
|2012| 1| 20| 50|
|2012| 1| 10| 20|
|2012| 1| 10| 50|
|2012| 1| 50| 20|
|2012| 1| 50| 10|
|2012| 2| 30| 5|
|2012| 2| 30| 60|
|2012| 2| 5| 30|
|2012| 2| 5| 60|
|2012| 2| 60| 30|
|2012| 2| 60| 5|
+----+-----+-----+-----+
我该怎么做?非常感谢
【问题讨论】:
标签: sql sql-server pyspark pyspark-sql aws-glue