无法在ORACLE 10G DATABASE上进行PIVOT

时间:2018-05-03 13:30:46

标签: sql oracle oracle10g

我试图用oracle 10G进行转播。屏幕截图1是表中的实际数据。 我试图获取像屏幕截图2这样的数据。请建议我PIGOT功能在10G不起作用。 Screen shot1Screen Shot2

1 个答案:

答案 0 :(得分:3)

您可以使用conditional aggregation

SELECT ANALYST
    ,COUNT(CASE WHEN status = 'unchecked'   THEN 1 END) AS unchecked
    ,COUNT(CASE WHEN status = 'observation' THEN 1 END) AS observation
    ,COUNT(CASE WHEN status = 'supervisor'  THEN 1 END) AS supervisor
    ,COUNT(CASE WHEN status = 'pending'     THEN 1 END) AS pending
    ,COUNT(CASE WHEN status = 'closed'      THEN 1 END) AS closed
    ,COUNT(*) AS Grand_Tot
FROM tab
GROUP BY ANALYST
ORDER BY ANALYST;

修改

  

我是否可以知道如何将Grand_Tot列和我正在尝试SUM(Grand_Tot)的查询相加,但将错误视为无效标识符。

一种方法是使用子查询:

SELECT sub.*, SUM(Grand_Tot) OVER() AS Sum_Grant_Tot
FROM (
    SELECT ANALYST
        ,COUNT(CASE WHEN status = 'unchecked'   THEN 1 END) AS unchecked
        ,COUNT(CASE WHEN status = 'observation' THEN 1 END) AS observation
        ,COUNT(CASE WHEN status = 'supervisor'  THEN 1 END) AS supervisor
        ,COUNT(CASE WHEN status = 'pending'     THEN 1 END) AS pending
        ,COUNT(CASE WHEN status = 'closed'      THEN 1 END) AS closed
        ,COUNT(*) AS Grand_Tot
    FROM tab
    GROUP BY ANALYST
) sub
ORDER BY ANALYST;