Databricks SQL语法中用于”where”语句的前六个月的部分。

huangapple go评论58阅读模式
英文:

Databricks SQL syntax for previous six months in where statement

问题

我正在尝试找出在Databricks的SQL查询的WHERE语句中查找过去六个月数据的方法,但是在语法方面遇到了很多问题。

现在我有:

Select * from table
where datediff(add_months(date_column, -6), date_column) = 1

这个查询没有报错,但没有返回任何结果。

英文:

I'm trying to figure out how to look for data in the last six months in the where statement of a SQL query in Databricks, but I'm having a lot of issues with the syntax.

Right now I have:

Select * from table
where datediff(add_months(date_column, -6), date_column) = 1

The query doesn't throw an error, but returns no results.

答案1

得分: 2

我认为你对DATEDIFF的期望是错误的。DATEDIFF告诉你两个日期之间的天数。在你的情况下,你正在将date_column与date_column减去6个月进行比较。这总是会得到6个月或约180天。

尝试这样做。

WHERE date_column > DATEADD(MONTH, -6, CURRENT_DATE())

也就是说,如果你的日期列大于当前日期减去6个月。

英文:

I think you're expecting the wrong thing from datediff. Datediff tells you the number of days between two dates. In your case, you're comparing your date_column to your date_column - 6 months. That's always going to be 6 months or ~180 days.

Try this.

WHERE date_column > DATEADD(MONTH, -6, CURRENT_DATE())

AKA, where your date column is greater than the current date minus 6 months.

huangapple
  • 本文由 发表于 2023年2月18日 01:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定