【问题标题】:Pyspark-SQL Sum Integer to Date (with sql)Pyspark-SQL Sum Integer to Date (with sql)
【发布时间】:2021-02-03 00:52:18
【问题描述】:

我想在给定日期添加任意天数,例如我想在今天的日期添加一天。

我有一个这样的数据框:

------------
|   date   |
------------
|2020-10-01|
------------

我想得到这样的数据框:

------------
|   date   |
------------
|2020-10-02|
------------

真正的代码包含在一个复杂的 sql 查询中,那么有效的结果只有 SQL 语句。

我已尝试使用此代码,尝试获取今天的第二天,但由于类型 date 和 int 之间的差异而无法正常工作,我认为我正在寻找类似于 python timedelta 但在 pyspark-sql 中的东西

spark.sql(f"SELECT to_date(now()) + 1")

错误:

cannot resolve '(to_date(current_timestamp()) + 1)' due to data type mismatch: differing types in '(to_date(current_timestamp()) + 1)' (date and int)

【问题讨论】:

    标签: apache-spark-sql


    【解决方案1】:

    经过一段时间的搜索,我找到了一个解决问题的函数:

    spark.sql("SELECT date_add(to_date(now()),1)").show()
    

    文档:

    date_add(Column start, int days)
    Returns the date that is days days after start
    

    https://spark.apache.org/docs/1.6.2/api/java/org/apache/spark/sql/functions.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      相关资源
      最近更新 更多