【问题标题】:PostgreSql - extract Month + year from datePostgreSql - 从日期中提取月份+年份
【发布时间】:2021-11-10 15:04:04
【问题描述】:

在 PostgreSQL 表中,我有两列 (int8),其中包含 Unix 时间戳。 [注意:此表不是我的,所以列可以更改为实际日期,因此在查询中进行转换。]

下面是我的查询的精简版。我需要做的是按月对条目进行分组。但在我下面的查询中,2020 年 11 月的条目与 2021 年 11 月的条目分组。

select 
DATE_PART('month',to_timestamp(e.startts)) as "Date", sum(e.checked)
from entries e
where 
e.startts >= date_part('epoch', '2020-10-01T15:01:50.859Z'::timestamp)::int8
and e.stopts <  date_part('epoch', '2021-11-08T15:01:50.859Z'::timestamp)::int8
group by "Date"

如何将“日期”作为月/年而不是月?所以例如 10/20 而不是 10。

【问题讨论】:

    标签: postgresql date


    【解决方案1】:

    使用to_char() 格式化时间戳值:

    to_char(to_timestamp(e.startts), 'mm/yy') as "Date"
    

    【讨论】:

    • 是否也可以添加变体?一年中的一周.. 例如W30-2021
    • 请看the manual你可能想要'iw-iyyy'
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2021-10-02
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多