【问题标题】:How to date_trunc by custom trimester in PostgreSQL?如何在 PostgreSQL 中通过自定义三个月来 date_trunc?
【发布时间】:2018-03-07 12:21:28
【问题描述】:

我正在开展一个项目,其中财政年度以三个月来衡量,尽管听起来很奇怪,但由于国家/地区的规定,每个三个月都是从当月的第 10 天而不是第 1 天开始计算的。因此,第一个三个月被认为是从 1 月 10 日开始,并在该月 9 日结束三个月后。有没有办法date_trunc 或简单地使用 PostgreSQL 中的这些自定义三个月来对时间戳列进行分组?

到目前为止,我只能按月/日/周查询数据,例如:

FROM myTable SELECT( SUM(price) as total, date_trunc('month', 'timpestamp') ) GROUP BY (total)

【问题讨论】:

    标签: postgresql timestamp intervals


    【解决方案1】:

    你可以加入结果代理准备的区间,例如:

    t=# select starts,starts+'3 month'::interval ends,mod(ord,4) from  generate_series('2015-10-01'::date,'2018-10-01'::date,'3 month'::interval) with ordinality t(starts,ord);
             starts         |          ends          | mod
    ------------------------+------------------------+-----
     2015-10-01 00:00:00+00 | 2016-01-01 00:00:00+00 |   1
     2016-01-01 00:00:00+00 | 2016-04-01 00:00:00+00 |   2
     2016-04-01 00:00:00+00 | 2016-07-01 00:00:00+00 |   3
     2016-07-01 00:00:00+00 | 2016-10-01 00:00:00+00 |   0
     2016-10-01 00:00:00+00 | 2017-01-01 00:00:00+00 |   1
     2017-01-01 00:00:00+00 | 2017-04-01 00:00:00+00 |   2
     2017-04-01 00:00:00+00 | 2017-07-01 00:00:00+00 |   3
     2017-07-01 00:00:00+00 | 2017-10-01 00:00:00+00 |   0
     2017-10-01 00:00:00+00 | 2018-01-01 00:00:00+00 |   1
     2018-01-01 00:00:00+00 | 2018-04-01 00:00:00+00 |   2
     2018-04-01 00:00:00+00 | 2018-07-01 00:00:00+00 |   3
     2018-07-01 00:00:00+00 | 2018-10-01 00:00:00+00 |   0
     2018-10-01 00:00:00+00 | 2019-01-01 00:00:00+00 |   1
    (13 rows)
    

    在这里,您可以使用 rownum order 的余数除以学期数来获得学期数(当然,您需要处理 0 - 要么称之为四,要么从负一学期开始,只需使用 mod(ord,4)+1 ,或使用case when等)

    【讨论】:

    • 感谢您的好评。我通过创建一个函数来解决这个问题,而不是像上面的 klin 示例一样。
    【解决方案2】:

    使用函数(或其中的表达式):

    create or replace function get_trimester(timestamp)
    returns integer language sql immutable as $$
        select (extract('month' from $1::date- 9)::int- 1)/ 3 + 1
    $$;
    

    检查某些日期的功能:

    with my_table(t) as (
    values 
        ('2017-01-09'::timestamp),
        ('2017-01-10'),
        ('2017-04-09'),
        ('2017-04-10'),
        ('2017-07-09'),
        ('2017-07-10'),
        ('2017-10-09'),
        ('2017-10-10'),
        ('2017-12-31')
    )
    
    select t, get_trimester(t)
    from my_table
    
              t          | get_trimester 
    ---------------------+---------------
     2017-01-09 00:00:00 |             4
     2017-01-10 00:00:00 |             1
     2017-04-09 00:00:00 |             1
     2017-04-10 00:00:00 |             2
     2017-07-09 00:00:00 |             2
     2017-07-10 00:00:00 |             3
     2017-10-09 00:00:00 |             3
     2017-10-10 00:00:00 |             4
     2017-12-31 00:00:00 |             4
    (9 rows)
    

    【讨论】:

    • 我最终使用了这种方法。我更容易生成日期系列。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多