如何基于多个条件使用SparkSQL在Spark DF中选择行

我对pyspark相对较新,并且有一个带有日期列“ Issue_Date”的spark数据框。 “ Issue_Date”列包含1970年至2060年的多个日期(由于错误)。从spark数据帧中,我从中创建了一个临时表,并且能够过滤2018年的数据。我还想包括2019年的数据(即多个条件)。有办法吗?我已经尝试了许多组合,但无法实现。感谢您提供任何形式的帮助,谢谢。

# Filter data from 2018
sparkdf3.createOrReplaceTempView("table_view")
sparkdf4 = spark.sql("select * from table_view where year(to_date(cast(unix_timestamp(Issue_Date,'MM/dd/yyyy') as timestamp))) = 2018")

sparkdf4.count()
xw7932 回答:如何基于多个条件使用SparkSQL在Spark DF中选择行

您尝试使用year(Issue_Date) >= 2018吗?:

sparkdf4 = spark.sql("select * from table_view where year(to_date(cast(unix_timestamp(Issue_Date,'MM/dd/yyyy') as timestamp))) >= 2018")

如果您的列中有错误,并且您想指定一个范围,则可以使用year IN (2018,2019)

sparkdf4 = spark.sql("select * from table_view where year(to_date(cast(unix_timestamp(Issue_Date,'MM/dd/yyyy') as timestamp))) in (2018,2019)")
本文链接:https://www.f2er.com/3154468.html

大家都在问