Q:

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)
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
So how to extract info 1-15 and 16-30/31 of timestamp field

A:

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;

