Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date Spark SQL-如何对链接到特定日期的日期范围内的值求和_Date_Apache Spark Sql_Sum_Date Range - Fatal编程技术网

Date Spark SQL-如何对链接到特定日期的日期范围内的值求和

Date Spark SQL-如何对链接到特定日期的日期范围内的值求和,date,apache-spark-sql,sum,date-range,Date,Apache Spark Sql,Sum,Date Range,我需要做一个过去30天的值的总和,相对于那个日期,每个商店的每一种产品都是唯一的。 假设所有月份都有30天: date|store|product|values 2020-06-30|Store1|Product1|1 2020-07-02|Store1|Product2|4 2020-07-01|Store2|Product1|3 2020-07-18|Store1|Product1|4 2020-07-18|Store1|Product2|2 2020-07-18|Store2|Produc

我需要做一个过去30天的值的总和,相对于那个日期,每个商店的每一种产品都是唯一的。 假设所有月份都有30天:

date|store|product|values
2020-06-30|Store1|Product1|1
2020-07-02|Store1|Product2|4
2020-07-01|Store2|Product1|3
2020-07-18|Store1|Product1|4
2020-07-18|Store1|Product2|2
2020-07-18|Store2|Product1|2
2020-07-30|Store1|Product1|1
2020-08-01|Store1|Product1|1
2020-08-01|Store1|Product2|1
2020-08-01|Store2|Product1|6
在第2020-08-01天的行中,将2020-08-20-30天与2020-08-19的值相加,并将其放入2020-08-20行中,如下所示: 第一行不包括“2020-06-30”,因为这是30多天前的事,而“2020-08-01”因为是同一天的事,这一行还在继续

date|store|product|sum_values_over_last_30_days_to_this_date
2020-08-01|Store1|Product1|5
2020-08-01|Store1|Product2|6
2020-08-01|Store2|Product1|5
....
在下面尝试了这个,但什么都没有:

spark.sql("""
SELECT 
a.date,
a.store,
a.product,
SUM(a.values) OVER (PARTITION BY a.product,a.store ORDER BY a.date BETWEEN a.date - INTERVAL '1' DAY AND a.date - INTERVAL '30' DAY) AS sum
FROM table a
""").show()

有人能帮我吗?

这是我对sampel数据帧的试用

+----------+------+--------+------+
|      date| store| product|values|
+----------+------+--------+------+
|2020-08-10|Store1|Product1|     1|
|2020-08-11|Store1|Product1|     1|
|2020-08-12|Store1|Product1|     1|
|2020-08-13|Store1|Product2|     1|
|2020-08-14|Store1|Product2|     1|
|2020-08-15|Store1|Product2|     1|
|2020-08-16|Store1|Product1|     1|
|2020-08-17|Store1|Product1|     1|
|2020-08-18|Store1|Product1|     1|
|2020-08-19|Store1|Product2|     1|
|2020-08-20|Store1|Product2|     1|
|2020-08-21|Store1|Product2|     1|
|2020-08-22|Store1|Product1|     1|
|2020-08-21|Store1|Product1|     1|
|2020-08-22|Store1|Product1|     1|
|2020-08-20|Store1|Product2|     1|
|2020-08-21|Store1|Product2|     1|
|2020-08-22|Store1|Product2|     1|
+----------+------+--------+------+

df.withColumn("date", to_date($"date"))
  .createOrReplaceTempView("table")

spark.sql("""
    SELECT
        date,
        store,
        product,
        COALESCE(SUM(values) OVER (PARTITION BY 1 ORDER BY date RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING), 0) as sum
    FROM table
""").show()

+----------+------+--------+---+
|      date| store| product|sum|
+----------+------+--------+---+
|2020-08-10|Store1|Product1|0.0|
|2020-08-11|Store1|Product1|1.0|
|2020-08-12|Store1|Product1|2.0|
|2020-08-13|Store1|Product2|3.0|
|2020-08-14|Store1|Product2|3.0|
|2020-08-15|Store1|Product2|3.0|
|2020-08-16|Store1|Product1|3.0|
|2020-08-17|Store1|Product1|3.0|
|2020-08-18|Store1|Product1|3.0|
|2020-08-19|Store1|Product2|3.0|
|2020-08-20|Store1|Product2|3.0|
|2020-08-20|Store1|Product2|3.0|
|2020-08-21|Store1|Product2|4.0|
|2020-08-21|Store1|Product1|4.0|
|2020-08-21|Store1|Product2|4.0|
|2020-08-22|Store1|Product1|6.0|
|2020-08-22|Store1|Product1|6.0|
|2020-08-22|Store1|Product2|6.0|
+----------+------+--------+---+

这是我对sampel数据帧的试用

+----------+------+--------+------+
|      date| store| product|values|
+----------+------+--------+------+
|2020-08-10|Store1|Product1|     1|
|2020-08-11|Store1|Product1|     1|
|2020-08-12|Store1|Product1|     1|
|2020-08-13|Store1|Product2|     1|
|2020-08-14|Store1|Product2|     1|
|2020-08-15|Store1|Product2|     1|
|2020-08-16|Store1|Product1|     1|
|2020-08-17|Store1|Product1|     1|
|2020-08-18|Store1|Product1|     1|
|2020-08-19|Store1|Product2|     1|
|2020-08-20|Store1|Product2|     1|
|2020-08-21|Store1|Product2|     1|
|2020-08-22|Store1|Product1|     1|
|2020-08-21|Store1|Product1|     1|
|2020-08-22|Store1|Product1|     1|
|2020-08-20|Store1|Product2|     1|
|2020-08-21|Store1|Product2|     1|
|2020-08-22|Store1|Product2|     1|
+----------+------+--------+------+

df.withColumn("date", to_date($"date"))
  .createOrReplaceTempView("table")

spark.sql("""
    SELECT
        date,
        store,
        product,
        COALESCE(SUM(values) OVER (PARTITION BY 1 ORDER BY date RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING), 0) as sum
    FROM table
""").show()

+----------+------+--------+---+
|      date| store| product|sum|
+----------+------+--------+---+
|2020-08-10|Store1|Product1|0.0|
|2020-08-11|Store1|Product1|1.0|
|2020-08-12|Store1|Product1|2.0|
|2020-08-13|Store1|Product2|3.0|
|2020-08-14|Store1|Product2|3.0|
|2020-08-15|Store1|Product2|3.0|
|2020-08-16|Store1|Product1|3.0|
|2020-08-17|Store1|Product1|3.0|
|2020-08-18|Store1|Product1|3.0|
|2020-08-19|Store1|Product2|3.0|
|2020-08-20|Store1|Product2|3.0|
|2020-08-20|Store1|Product2|3.0|
|2020-08-21|Store1|Product2|4.0|
|2020-08-21|Store1|Product1|4.0|
|2020-08-21|Store1|Product2|4.0|
|2020-08-22|Store1|Product1|6.0|
|2020-08-22|Store1|Product1|6.0|
|2020-08-22|Store1|Product2|6.0|
+----------+------+--------+---+
您可以尝试自连接而不是窗口函数,也许这种连接会起作用-

SELECT 
    a.date, 
    a.store,
    a.product,
    SUM(IFNULL(b.value,0))
FROM
    table a
LEFT JOIN
    (
        SELECT
            a.date, 
            a.store,
            a.product,
            a.value
        FROM
            table  a
    )b
ON
    a.store = b.store
AND
    a.product = b.product
AND
    a.date > b.date - INTERVAL 30 DAYS
AND a.date <= b.date
GROUP BY 
    1,2,3
确保对内部查询中的值求和,直到今天为止进行求和。

您可以尝试自联接而不是窗口函数,也许这种联接会起作用-

SELECT 
    a.date, 
    a.store,
    a.product,
    SUM(IFNULL(b.value,0))
FROM
    table a
LEFT JOIN
    (
        SELECT
            a.date, 
            a.store,
            a.product,
            a.value
        FROM
            table  a
    )b
ON
    a.store = b.store
AND
    a.product = b.product
AND
    a.date > b.date - INTERVAL 30 DAYS
AND a.date <= b.date
GROUP BY 
    1,2,3

确保对内部查询中的值求和,直到今天为止都要求和。

a.values列在哪里?在你的桌子上?请给我们正确的表格a.values列在哪里?在你的桌子上?请给我们正确的桌子谢谢@Assaf!!只是我必须修改的加入日期和a.日期-间隔“30”天b.日期和注意:每个日期、商店、产品和我的维度必须有一行。或者在加入之前,只需按日期、商店、产品对组值求和。@PauloHFM很高兴这有帮助!谢谢你的额外留言。谢谢@Assaf!!只是我必须修改的加入日期和a.日期-间隔“30”天b.日期和注意:每个日期、商店、产品和我的维度必须有一行。或者在加入之前,只需按日期、商店、产品对组值求和。@PauloHFM很高兴这有帮助!谢谢你的额外通知。