如何过滤当年年初和前三年的数据



我目前有:

last3years_dates = ('2020-01-01 08:56:00', '2020-10-29 09:18:00')
df_NIRS = df_NIRS.where(F.col('RaisedDate').between(*last3years_dates))

"2020-01-01 08:56:00"one_answers"2020-10-29 09:18:00"是占位符。如何在不硬编码的情况下从今年年初(即"2020-01-01 00:00:00"(到前三年(2017-01-01 00:00:00(进行筛选。可能使用current_timestamp()

df.filter(
F.col('RaisedDate').between(
F.to_date(
(F.year(F.current_timestamp()) - F.lit(3)).cast('string'), 'yyyy'
),
F.to_date(
F.year(F.current_timestamp()).cast('string'), 'yyyy'
)
)
)

请尝试以下代码。

df
.filter("dtCol between (current_timestamp - INTERVAL + 3 YEARS) AND date_trunc('Year',current_timestamp)")
.show()

最新更新