Apache spark 用pyspark计算YTD和MTD

Apache spark 用pyspark计算YTD和MTD,apache-spark,pyspark,apache-spark-sql,Apache Spark,Pyspark,Apache Spark Sql,我已经试着实施了大约一个月。 仅使用其他堆栈溢出问题中的一些示例数据 FinancialYearStart MonthOfFinancialYear SalesTotal 2015 1 10 2015 2 10 2015 5 10

我已经试着实施了大约一个月。 仅使用其他堆栈溢出问题中的一些示例数据

   FinancialYearStart  MonthOfFinancialYear  SalesTotal
            2015                     1          10
            2015                     2          10
            2015                     5          10
            2015                     6          50
            2016                     1          10
            2016                     3          20
            2016                     2          30
            2017                     6          70
            2017                     7          80
我们如何使用它来计算每个月的年初至今销售总额

FinancialYearStart  MonthOfFinancialYear  SalesTotal  YTDTotal
            2015                     1          10        10
            2015                     2          10        20
            2015                     5          10        30
            2015                     6          50        50
            2016                     1          10        60
            2016                     3          20        80
            2016                     2          30       110
            2017                     6          70        70
            2017                     7          80       150
更具体地说,我们如何使用group by或windowing函数计算这些指标

例如:

Year Month Customer TotalMonthlySales
2015 1 Dog 10
2015 2 Dog 10
2015 3 Cat 20
2015 4 Dog 30
2015 5 Cat 10
2015 7 Cat 20
2015 7 Dog 10
2016 1 Dog 40
2016 2 Dog 20
2016 3 Cat 70
2016 4 Dog 30
2016 5 Cat 10
2016 6 Cat 20
2016 7 Dog 10
将提供:

Year Month Customer TotalMonthlySales YTDSales
2015 1 Dog 10 10
2015 2 Dog 10 20
2015 3 Cat 20 20
2015 4 Dog 30 50
2015 5 Cat 10 30
2015 7 Cat 20 40
2015 7 Dog 10 60
2016 1 Dog 40 40
2016 2 Dog 20 60
2016 3 Cat 70 70
2016 4 Dog 30 90
2016 5 Cat 10 80
2016 6 Cat 20 100
2016 7 Dog 10 100
现在,我使用滚动窗口函数,使用以下方法计算过去4周和13周

w = (Window().partitionBy(col("number"),col("id")).orderBy(F.col("timestampGMT").cast('long')).rangeBetween(-days(27), 0))
df1 = df.withColumn('lw_4weeksCY_SALES_DOLLARS', F.sum("CY_SALES_DOLLARS").over(w)).withColumn('lw_4weeksCY_SALES_UNITS', F.sum("CY_SALES_UNITS")
您可以使用window函数在sparksql中实现

希望这对你有帮助。如果您有任何疑问,请告诉我

df.show()
+----+-----+--------+-----------------+
|Year|Month|Customer|TotalMonthlySales|
+----+-----+--------+-----------------+
|2015|    1|     Dog|               10|
|2015|    2|     Dog|               10|
|2015|    3|     Cat|               20|
|2015|    4|     Dog|               30|
|2015|    5|     Cat|               10|
|2015|    7|     Cat|               20|
|2015|    7|     Dog|               10|
|2016|    1|     Dog|               40|
|2016|    2|     Dog|               20|
|2016|    3|     Cat|               70|
|2016|    4|     Dog|               30|
|2016|    5|     Cat|               10|
|2016|    6|     Cat|               20|
|2016|    7|     Dog|               10|
+----+-----+--------+-----------------+

df.registerTempTable("test")
sql("select *, sum(TotalMonthlySales) over(partition by year,Customer order by month) tt from test ").orderBy("year","month").show()
+----+-----+--------+-----------------+---+
|Year|Month|Customer|TotalMonthlySales| tt|
+----+-----+--------+-----------------+---+
|2015|    1|     Dog|               10| 10|
|2015|    2|     Dog|               10| 20|
|2015|    3|     Cat|               20| 20|
|2015|    4|     Dog|               30| 50|
|2015|    5|     Cat|               10| 30|
|2015|    7|     Cat|               20| 50|
|2015|    7|     Dog|               10| 60|
|2016|    1|     Dog|               40| 40|
|2016|    2|     Dog|               20| 60|
|2016|    3|     Cat|               70| 70|
|2016|    4|     Dog|               30| 90|
|2016|    5|     Cat|               10| 80|
|2016|    6|     Cat|               20|100|
|2016|    7|     Dog|               10|100|
+----+-----+--------+-----------------+---+