在 SQL 中使用 DATEADD() 过滤过去 3 个月的数据

发布于 2025-01-15 06:42:01 字数 699 浏览 2 评论 0原文

我试图显示过去三个月的数据,所以已经使用了

SELECT my_date::date AS my_date, my_col
FROM my_table     
WHERE my_date >= DATEADD(MONTH, -3, GETDATE())
GROUP BY my_date
ORDER BY my_date DESC

但我收到以下错误

ERROR:  column "month" does not exist
LINE 10: WHERE process_date >= DATEADD(MONTH, -3, GETDATE())
                                       ^
SQL state: 42703
Character: 651

我不确定为什么它不喜欢 MONTH,我尝试使用 DATEADD() ,如下所示 https://www.w3schools.com/sql/func_sqlserver_dateadd.asp DATEADD(间隔、数字、日期)

我应该如何过滤过去 3 个月的数据以及为什么 DATEADD() 似乎不起作用?

I am trying to show data from the last three months so have used

SELECT my_date::date AS my_date, my_col
FROM my_table     
WHERE my_date >= DATEADD(MONTH, -3, GETDATE())
GROUP BY my_date
ORDER BY my_date DESC

But I get the following error

ERROR:  column "month" does not exist
LINE 10: WHERE process_date >= DATEADD(MONTH, -3, GETDATE())
                                       ^
SQL state: 42703
Character: 651

I am not sure why it doesn't like MONTH, I am trying to use DATEADD() as shown here https://www.w3schools.com/sql/func_sqlserver_dateadd.asp DATEADD(interval, number, date).

How should I be filtering data for the last 3 months and why does DATEADD() not seem to be working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2025-01-22 06:42:01

感谢@VvdL 的评论。

PostgreSQL 使用不同的命令。更改为以下并有效。

WHERE my_date >= current_date - INTERVAL '3 months'

Thanks to @VvdL for their comment.

PostgreSQL uses a different command. Changed to below and works.

WHERE my_date >= current_date - INTERVAL '3 months'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文