今天在群里看到一朋友提问
问题是截图,截图如下
用Postgresql的crosstab很快就能做出来
CREATE TABLE sales ( year integer,-- 年 month integer,-- 月 counts integer -- 日 ) WITH ( OIDS=FALSE ); ALTER TABLE sales OWNER TO postgres; COMMENT ON TABLE sales IS '销售报表'; COMMENT ON COLUMN sales.year IS '年'; COMMENT ON COLUMN sales.month IS '月'; COMMENT ON COLUMN sales.counts IS '日'; insert into sales values(1991,1,11),(1991,2,12),3,13),4,14),(1992,21),22),23),24); CREATE EXTENSION tablefunc; select * from sales; SELECT * FROM crosstab('select year,month,counts from sales order by 1','select distinct month from sales order by 1') AS t ("年" integer,"一月" integer,"二月" integer,"三月" integer,"四月" integer);
另外需要注意下crosstab这个EXTENSION需要create才可以用。
结果如下截图