PostgreSQL交叉表查询
有没有人知道如何在PostgreSQL中创建交叉表查询?
例如我有下面的表: Section Status Count A Active 1 A Inactive 2 B Active 4 B Inactive 5 我想查询返回以下交叉表: Section Active Inactive A 1 2 B 4 5 这可能吗?
每个数据库安装一次
additional module tablefunc ,它提供函数crosstab()。从PostgreSQL 9.1你可以使用
CREATE EXTENSION :
CREATE EXTENSION tablefunc; 改进的测试用例 CREATE TEMP TABLE t ( section text,status text,ct integer -- don't use "count" as column name. ); INSERT INTO t VALUES ('A','Active',1),('A','Inactive',2),('B',4),5),('C',7); -- no row for C with 'Active' > count是一个reserved word,不要使用它作为列名。 简单形式 – 不适合缺少属性 带有1个参数的交叉表(文本): SELECT * FROM crosstab( 'SELECT section,status,ct FROM t ORDER BY 1,2') -- needs to be "ORDER BY 1,2" here AS ct ("Section" text,"Active" int,"Inactive" int); 返回: Section | Active | Inactive ---------+--------+---------- A | 1 | 2 B | 4 | 5 C | 7 | >无需转换和重命名。 安全形式 交叉表(文本,文本)带有2个参数: SELECT * FROM crosstab( 'SELECT section,ct FROM t ORDER BY 1,2' -- could also just be "ORDER BY 1" here,$$VALUES ('Active'::text),('Inactive')$$) AS ct ("Section" text,"Inactive" int); 返回: Section | Active | Inactive ---------+--------+---------- A | 1 | 2 B | 4 | 5 C | | 7 >注意C的正确结果。 'SELECT DISTINCT attribute FROM tbl ORDER BY 1' 这是在手册。 因为你必须拼写列定义列表中的所有列(除了预定义的crosstabN()变体),在VALUES表达式中提供一个短列表通常更有效率,如我演示: $$VALUES ('Active'::text),('Inactive')$$) 要么: $$SELECT unnest('{Active,Inactive}'::text[])$$ -- shorter for long lists 这不是在手册。 高级示例 > Pivot on Multiple Columns using Tablefunc – 也演示了提到的“额外列”。 纠正 previously accepted answer by Jeremiah已过时。 >函数crosstab(text,integer)的变体已过时。第二个整数参数被忽略。我引用current manual:
>无需铸造和重命名。
(仅适用于在那里使用的一参数形式的crosstab())。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |