【问题标题】:Create Postgresql index with date_trunc使用 date_trunc 创建 Postgresql 索引
【发布时间】:2019-04-18 17:50:49
【问题描述】:

与此question 相关。我想创建一个返回与此查询相同的输出的索引;

--takes 2005-10-12
select date_trunc('month',table_withdates.dateoftransfer::date)::Date
from table_withdates;
--returns 2005-10-01

下面将索引但返回的时间戳添加到日期,这不是我想要的。

create index on table_withdates  (date_trunc('month', dateoftransfer::timestamp));
--returns 2005-10-01 00:00:00

是否可以创建一个以该格式返回没有时间戳的日期的索引

【问题讨论】:

  • 谢谢,但是当我运行select date_trunc('month',table_withdates.dateoftransfer::date) from table_withdates; 时它返回2005-10-01 00:00:00+01 会是索引吗?
  • 转移日期是date

标签: postgresql postgresql-11


【解决方案1】:

Quote from the manual

返回值是时间戳或间隔类型,所有比所选字段不重要的字段都设置为零

所以您需要将date_trunc 的结果转换为日期:

create index on table_withdates  (date_trunc('month', dateoftransfer)::date);

请注意,为了使索引可用于查询,您需要使用完全相同的表达式,例如:

where date_trunc('month', dateoftransfer)::date  = ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多