好久没写博客了 也好久没写代码了 拿这个充充数吧 哈哈
今天在群里看到一朋友提问
问题是截图,截图如下
用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),(1991,3,13),(1991,4,14),(1992,1,21),(1992,2,22),(1992,3,23),(1992,4,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才可以用。
结果如下截图