简体   繁体   English

如何从PostgreSQL中的时间戳中提取句点

[英]How to extract periods from timestamp in postgresql

I need to extract and group information of timestamp from 1st day of the month to 15th dat (including) and from 16th day up to the end of the month (including last day) 我需要提取和分组从每月的第一天到第15天(包括)以及从第16天到该月末(包括最后一天)的时间戳信息

I made this for every weeks only. 我只每星期做一次。 Here is the code 这是代码

 SELECT
    extract(week from dttrnreqdate) AS period, 
    SUM(bonusesCharged - bonusesCharged_rev) AS bonusesChargedForPeriod,
    SUM(bonusesWithdrawn - bonusesWithdrawn_rev) AS bonusesWithdrawnForPeriod
FROM
    ls.vstatement AS v
WHERE
    v.dttrnreqdate >= to_timestamp('2014-12-01 00:00:00', 'yyyy-mm-dd 00:00:00') AND
    v.dttrnreqdate < to_timestamp('2015-01-07 00:00:00', 'yyyy-mm-dd 00:00:00')
GROUP BY
    extract(week from dttrnreqdate)
ORDER BY
    extract(week from dttrnreqdate)
 ASC

where dttrnreqdate is a timestamp bonusesCharged, bonusesCharged_rev, bonusesWithdrawn, bonusesWithdrawn_rev - int 其中dttrnreqdatetimestamp bonusesCharged, bonusesCharged_rev, bonusesWithdrawn, bonusesWithdrawn_rev - int

So how to extract info 1-15 and 16-30/31 of timestamp field 那么如何提取时间戳字段的信息1-15和16-30 / 31

I'm pretty sure this answers your question, but I'm not sure it will solve your problem. 我很确定这可以回答您的问题,但是我不确定它是否可以解决您的问题。

SELECT
    case when extract(day from dttrnreqdate) <= 15 then 'grp1'
         else 'grp2'
    end as grp,
    SUM(bonusesCharged - bonusesCharged_rev) AS bonusesChargedForPeriod,
    SUM(bonusesWithdrawn - bonusesWithdrawn_rev) AS bonusesWithdrawnForPeriod
FROM
    ls.vstatement AS v
WHERE
    v.dttrnreqdate >= to_timestamp('2014-12-01 00:00:00', 'yyyy-mm-dd 00:00:00') AND
    v.dttrnreqdate < to_timestamp('2015-01-07 00:00:00', 'yyyy-mm-dd 00:00:00')
GROUP BY
    grp
ORDER BY
    grp
 ASC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM