如何从postgresql中的时间戳中提取句点
How to extract periods from timestamp in postgresql
我需要提取和分组时间戳信息从当月的第1天到第15天(含)和从第16天到月底(包括最后一天)
我每周都做这个。这是代码
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
在哪里
dttrnreqdate
是 timestamp
bonusesCharged, bonusesCharged_rev, bonusesWithdrawn, bonusesWithdrawn_rev
- int
那么如何提取时间戳字段的信息1-15和16-30/31
我很确定这会回答您的问题,但我不确定它是否会解决您的问题。
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;
我需要提取和分组时间戳信息从当月的第1天到第15天(含)和从第16天到月底(包括最后一天)
我每周都做这个。这是代码
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
在哪里
dttrnreqdate
是 timestamp
bonusesCharged, bonusesCharged_rev, bonusesWithdrawn, bonusesWithdrawn_rev
- int
那么如何提取时间戳字段的信息1-15和16-30/31
我很确定这会回答您的问题,但我不确定它是否会解决您的问题。
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;