我有一张桌子,我正在计算每日数据库的增长。
time_stmp| datname| datsize
----------------------------+----
2018-05-1 | test | 35 MB
2018-05-2 | test | 38 MB
2018-05-3 | test | 49 MB
.
.
.
.
2018-05-29 | test | 57 MB
2018-05-31 | test | 62 MB
甚至,我可以使用以下查询
计算月末的总增长select
time_stmp,datname,pg_size_pretty(sum(datsize))
from
testtable
where
to_char(time_stmp, 'YYYY-MM-DD') = to_char(date_trunc('month', CURRENT_DATE) + interval '1 month - 1 day','YYYY-MM-DD')
group by 1,2;
time_stmp| datname| datsize
----------------------------+----
2018-05-31 | test | 62 MB
但我无法在每个月找到确切的总增长情况并生成单个表输出,如下所示
time_stmp| datname| datsize
----------------------------+----
2018-01-1 | test | 35 MB
2018-02-1 | test | 50 MB
2018-03-1 | test | 75 MB
.
.
.
2018-12-1 | test | 300 MB
感谢您的回复..
答案 0 :(得分:0)
尝试此查询:
SELECT
time_stmp, datname, pg_size_pretty(datsize)
FROM
testtable
WHERE
to_char(time_stmp + interval '1 day', 'YYYY-MM-DD') =
to_char(date_trunc('month', time_stmp) + interval '1 month', 'YYYY-MM-DD');
这是一个简短的演示,显示时间戳逻辑是正确的,并且正确识别月份的末尾: