【问题标题】:Finding the last date of the previous quarter from current date in PostGreSQL在 PostGreSQL 中从当前日期查找上一季度的最后一个日期
【发布时间】:2022-07-21 23:30:26
【问题描述】:

例如:如果我的当前日期是 2022-07-21,我的查询应该给我 2022-06-30。 我可以在 IBM DB2 中轻松做到这一点,但在 postgresql 中却遇到了困难。

【问题讨论】:

  • 为什么上一季度的最后一个日期不是 6 月 30 日?
  • 它的错字,是的,它是 6 月 30 日。错别字

标签: postgresql postgresql-9.3 postgresql-9.4 postgresql-9.5 postgresql-9.2


【解决方案1】:

您可以将当前日期截断为其季度,然后从中删除 1 天(并可能回溯到日期):

-- You really only need the last column, the other two just show the different steps in the process
SELECT DATE_TRUNC('quarter', CURRENT_DATE)
     , DATE_TRUNC('quarter', CURRENT_DATE) - '1 day'::interval
     , (DATE_TRUNC('quarter', CURRENT_DATE) - '1 day'::INTERVAL)::DATE

输出

+---------------------------------+---------------------------------+----------+
|date_trunc                       |?column?                         |date      |
+---------------------------------+---------------------------------+----------+
|2022-07-01 00:00:00.000000 +00:00|2022-06-30 00:00:00.000000 +00:00|2022-06-30|
+---------------------------------+---------------------------------+----------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2016-04-24
    相关资源
    最近更新 更多