PostgreSQL 9.5:计算SUM并存储到HSTORE中

时间:2017-05-03 12:01:34

标签: postgresql hstore

我的下表有三列:

CREATE TABLE Cal_3Month
(
ColID int,
ColDate timestamp,
ColValue bigint
);

INSERT INTO Cal_3Month values(1,'2017-01-31 00:00:00',100);
INSERT INTO Cal_3Month values(2,'2017-04-22 00:00:00',123);
INSERT INTO Cal_3Month values(3,'2017-04-02 00:00:00',990);
INSERT INTO Cal_3Month values(4,'2017-03-22 00:00:00',890);
INSERT INTO Cal_3Month values(5,'2017-02-11 00:00:00',1000);
INSERT INTO Cal_3Month values(6,'2017-01-02 00:00:00',2300);
INSERT INTO Cal_3Month values(1,'2017-03-27 00:00:00',238);
INSERT INTO Cal_3Month values(2,'2017-04-22 00:00:00',400);
INSERT INTO Cal_3Month values(1,'2017-02-12 00:00:00',500);
INSERT INTO Cal_3Month values(1,'2017-02-23 00:00:00',50);
INSERT INTO Cal_3Month values(7,'2017-02-25 00:00:00',780);
INSERT INTO Cal_3Month values(8,'2017-02-26 00:00:00',90);
INSERT INTO Cal_3Month values(2,'2017-03-22 00:00:00',78);

SELECT * FROM Cal_3Month order by colid;

给定记录

colid   coldate         colvalue
-----------------------------------------------
1   2017-01-31 00:00:00 100
1   2017-03-27 00:00:00 238
1   2017-02-12 00:00:00 500
1   2017-02-23 00:00:00 50
2   2017-03-22 00:00:00 78
2   2017-04-22 00:00:00 400
2   2017-04-22 00:00:00 123
3   2017-04-02 00:00:00 990
4   2017-03-22 00:00:00 890
5   2017-02-11 00:00:00 1000
6   2017-01-02 00:00:00 2300
7   2017-02-25 00:00:00 780
8   2017-02-26 00:00:00 90

注意:我想计算前3个月{/ 1}} sum的{​​{1}} ,并希望将其存储 转换为colvalue格式。

预期结果

colid

我的尝试:

HSTORE

结果

colid   date and sum value(hstore)
-----------------------------------------------
1   "201702"=>"550","201703"=>"238"
2   "201703"=>"78","201704"=>"523"
3   "201704"=>"990"
4   "201703"=>"890"
5   "201702"=>"1000"
6
7   "201702"=>"780"
8   "201702"=>"90"

1 个答案:

答案 0 :(得分:1)

https://gitpitch.com/user-name/repository-name/sub-folder