【问题标题】:PostgreSQL: Select only past three months of dataPostgreSQL:仅选择过去三个月的数据
【发布时间】:2022-01-19 17:31:40
【问题描述】:

我试图从表中仅选择过去三个月的数据。我尝试了以下方法:

SELECT *
FROM cte_data
WHERE  DATEDIFF(MONTH, my_timestamp, GETDATE()) <= 3

这是返回错误: 错误:“月”列不存在位置:1182

我的日期格式如下: 2021 年 12 月 16 日下午 2:40

我怎样才能做到这一点?

注意:我在元数据库上使用 PostgreSQL。

【问题讨论】:

  • getdate() 闻起来像 ms sql server。 postgres 有 now()
  • @LukStorms:Postgres 也没有datediff() 函数

标签: sql postgresql


【解决方案1】:

正如 in the manual 所记录的,Postgres 中没有 datediff() 函数。你只需减去一个interval

select *
from cte_data
where my_timestamp >= current_timestamp - interval '3 month';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多