PostgreSQL 9.3:数据透视表查询

时间:2015-03-16 07:18:41

标签: sql postgresql pivot-table postgresql-9.3 table-functions

我想显示下面给定表格的数据透视表(交叉表)。

表:Employee

CREATE TABLE Employee
(
Employee_Number varchar(10),
Employee_Role varchar(50),
Group_Name varchar(10)
);

插入:

INSERT INTO Employee VALUES('EMP101','C# Developer','Group_1'),
                           ('EMP102','ASP Developer','Group_1'),
                           ('EMP103','SQL Developer','Group_2'),
                           ('EMP104','PLSQL Developer','Group_2'),
                           ('EMP101','Java Developer',''),
                           ('EMP102','Web Developer','');

现在我想显示上述数据的数据透视表,如下所示:

预期结果

Employee_Number     TotalRoles      TotalGroups       Available     Others     Group_1     Group_2
---------------------------------------------------------------------------------------------------
   EMP101               2                2                1           1           1           0
   EMP102               2                2                1           1           1           0
   EMP103               1                2                1           0           0           1 
   EMP104               1                2                1           0           0           1

解释:我想展示每位员工所拥有的Employee_NumberTotalRolesTotalGroups显示给所有员工的Available,显示员工可用 在多少组中,Others必须显示该员工在其他组中也可用 group_name尚未分配,最后Group_Names必须以数据透视表格式显示。

2 个答案:

答案 0 :(得分:1)

您可以使用crosstab功能。

首先,如果您还没有添加tablefunc扩展名:

CREATE EXTENSION tablefunc;

交叉表函数要求您传递一个查询,返回您需要转动的数据,然后输出输出中的列列表。 (以其他方式"告诉我你想要的输入和输出格式")。排序顺序很重要!

在您的情况下,输入查询非常复杂 - 我认为您需要加载单独的查询,然后UNION ALL它们以获取所需的数据。我不完全确定你如何计算价值" TotalGroups"和"可用",但您可以在相关位置修改以下内容以获得所需内容。

 SELECT * FROM crosstab(

'SELECT employee_number, attribute, value::integer AS value FROM (with allemployees AS (SELECT distinct employee_number FROM employee) -- use a CTE to get distinct employees

SELECT employee_number,''attr_0'' AS attribute,COUNT(distinct employee_role) AS value FROM employee GROUP BY employee_number -- Roles by employee
UNION ALL
SELECT employee_number,''attr_1'' AS attribute,value from allemployees, (select count (distinct group_name) as value from employee where group_name <> '''') a
UNION ALL
SELECT employee_number,''attr_2'' AS attribute, COUNT(distinct group_name) AS value FROM employee where group_name <> '''' GROUP BY employee_number -- Available, do not know how this is calculate
UNION ALL

SELECT a.employee_number, ''attr_3'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- other groups. Use a LEFT JOIN to avoid nulls in the output
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name ='''' GROUP BY employee_number) b on a.employee_number = b.employee_number
UNION ALL
SELECT a.employee_number, ''attr_4'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- group 1
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name =''Group_1'' GROUP BY employee_number) b on a.employee_number = b.employee_number
UNION ALL
SELECT a.employee_number, ''attr_5'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- group 2
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name =''Group_2'' GROUP BY employee_number) b on a.employee_number = b.employee_number) a order by 1,2')

AS ct(employee_number varchar,"TotalRoles" integer,"TotalGroups" integer,"Available" integer, "Others" integer,"Group_1" integer, "Group_2" integer)

答案 1 :(得分:1)

SELECT * FROM crosstab(
      $$SELECT grp.*, e.group_name
             , CASE WHEN e.employee_number IS NULL THEN 0 ELSE 1 END AS val
        FROM  (
           SELECT employee_number
                , count(employee_role)::int            AS total_roles
                , (SELECT count(DISTINCT group_name)::int
                   FROM   employee
                   WHERE  group_name <> '')            AS total_groups
                , count(group_name <> '' OR NULL)::int AS available
                , count(group_name =  '' OR NULL)::int AS others
           FROM   employee
           GROUP  BY 1
           ) grp
        LEFT   JOIN employee e ON e.employee_number = grp.employee_number
                              AND e.group_name <> ''
        ORDER  BY grp.employee_number, e.group_name$$
     ,$$VALUES ('Group_1'::text), ('Group_2')$$
   ) AS ct (employee_number text
          , total_roles  int
          , total_groups int
          , available    int
          , others       int
          , "Group_1"    int
          , "Group_2"    int);

SQL Fiddle演示了基本查询,但没有安装在sqlfiddle.com上的交叉表步骤

交叉表的基础知识:

这个交叉表中的特别之处:所有&#34;额外&#34;列。这些列位于&#34;行名称&#34;之后的中间。但之前&#34;类别&#34;和&#34;价值&#34;:

再一次,如果你有一组动态的组,你需要动态构建这个语句并在第二次调用中执行它: