简体   繁体   English

获取spark sql中上次特定月份的结果的最简单方法是什么?

[英]What is the simplest way to get results for last time specific month occurred in spark sql?

I have a month as an input and need to generate sql query that will get results for that month last occurrence - so previous or current year depending on the month - but I want it to be as human readable as possible.我有一个月作为输入,需要生成 sql 查询,该查询将获得该月上次出现的结果 - 因此根据月份而定,上一年或当前年份 - 但我希望它尽可能地易于人类阅读。 Tried this but it wont work as you can't use CASE that way:试过这个,但它不会工作,因为你不能那样使用 CASE:

WHERE month(DateF) = 11
AND year(DateF) = (
CASE WHEN 11 > month(CURRENT_DATE) 
THEN year(CURRENT_DATE)-1
ELSE year(CURRENT_DATE)
)

You can use the Spark SQL in-built functions to work with date and time.您可以使用 Spark SQL 内置函数来处理日期和时间。 These functions give capability to work with different dates, in different formats.这些函数提供了处理不同日期、不同格式的能力。

Refer: https://www.obstkel.com/spark-sql-date-functions参考: https : //www.obstkel.com/spark-sql-date-functions

I think you can do that by using current_date and a day column (if you have it):我认为您可以通过使用 current_date 和 day 列(如果有)来做到这一点:

WHERE month(dateF) = X AND day >=  date_add(current_date(), -365)

Will this be possible for you ?这对你有可能吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM