how to get row level totals with pivot in oracle sql

时间:2016-04-04 17:31:55

标签: oracle pivot

I have a table which has the following entries

ID |pod  | column   | value
------------------------
1  |A    | status   | DONE
2  |B    | status   | FAILED
1  |A    | progress | Green
2  |B    | progress | Red

i want output as

ID | DONE | FAILED | STATUS_TOTAL | GREEN | RED | PROGRESS_TOTAL
1  | 1    | 0      | 1            | 1     | 0   | 1
2  | 0    | 1      | 1            | 0     | 1   | 1

I do not want column total only row total. With my query i have got

ID | DONE | FAILED | GREEN | RED
1  | 1    | 0      | 1     | 0
2  | 0    | 1      | 0     | 1

The query i have used is

Select * from (SELECT  ID AS "SCHEDULE_ID",POD AS "POD_NAME",VALUE
FROM TABLE 
WHERE METRIC_NAME='metric_name' and column IN ('status','progress' ))
PIVOT (COUNT( DISTINCT POD_NAME) FOR VALUE in 
 ('DONE' AS "DONE",'SKIP' AS "SKIP",'FAILED' AS "FAILED",
  'GREEN' AS GREEN,'RED' AS "RED",'YELLOW' AS "YELLOW"));

3 个答案:

答案 0 :(得分:0)

You can always calculate the total in the subquery using rollup and pivot it as usual;

SELECT * FROM (
  SELECT ID AS "SCHEDULE_ID", pod AS "POD_NAME", 
    CASE WHEN GROUPING(value) = 0 
         THEN value 
         ELSE CASE WHEN col='status' 
              THEN 'STATUS_TOTAL' 
              ELSE 'PROGRESS_TOTAL' 
              END 
         END value
  FROM mytable
  WHERE col IN ('status','progress' )
  GROUP BY id, pod, col, ROLLUP(value)
)
PIVOT (COUNT( DISTINCT POD_NAME) FOR VALUE in 
 ('DONE' AS "DONE", 'SKIP' AS "SKIP", 'FAILED' AS "FAILED",
  'STATUS_TOTAL' AS "STATUS_TOTAL",
  'GREEN' AS GREEN,'RED' AS "RED",'YELLOW' AS "YELLOW", 
  'PROGRESS_TOTAL' AS "PROGRESS_TOTAL"));

答案 1 :(得分:0)

Perhaps something like this..

SELECT a.schedule_id,
   a.pod_name,
   a.done,
   a.skip,
   a.failed,
   (a.done + a.skip + a.failed) AS status_total,
   a.green,
   a.red,
   a.yellow,
   (a.green + a.red + a.yellow) AS progress_total
 FROM (SELECT schedule_id,
           pod_name,
           DONE,
           SKIP,
           FAILED,
           GREEN,
           RED,
           YELLOW
      FROM (SELECT ID AS "SCHEDULE_ID", POD AS "POD_NAME", VALUE
              FROM TABLE WHERE METRIC_NAME='metric_name' and column IN ('status','progress'))
PIVOT (COUNT( DISTINCT POD_NAME) FOR VALUE in
('DONE' AS DONE,'SKIP' AS SKIP,'FAILED' AS FAILED,
'GREEN' AS GREEN,'RED' AS RED,'YELLOW' AS YELLOW)))a;

答案 2 :(得分:0)

如果不使用PIVOT,您可以执行此操作,这可能会有更好的效果。

SELECT  Id,
        COUNT(CASE WHEN column = 'status' AND value = 'DONE' THEN 1 END) AS DONE,
        COUNT(CASE WHEN column = 'status' AND value = 'FAILED' THEN 1 END) AS FAILED,
        COUNT(CASE WHEN column = 'status' THEN 1 END) AS STATUS_TOTAL,
        COUNT(CASE WHEN column = 'progress' AND value = 'Green' THEN 1 END) AS GREEN,
        COUNT(CASE WHEN column = 'progress' AND value = 'Red' THEN 1 END) AS RED,
        COUNT(CASE WHEN column = 'progress' THEN 1 END) AS PROGRESS_TOTAL
FROM    TABLE
GROUP BY Id

如果您只想为每个ID计算一次值,则可以添加DISTINCT

SELECT  Id,
        COUNT(DISTINCT CASE WHEN column = 'status' AND value = 'DONE' THEN value END) AS DONE,
        COUNT(DISTINCT CASE WHEN column = 'status' AND value = 'FAILED' THEN value END) AS FAILED,
        COUNT(DISTINCT CASE WHEN column = 'status' THEN value END) AS STATUS_TOTAL,
        COUNT(DISTINCT CASE WHEN column = 'progress' AND value = 'Green' THEN value END) AS GREEN,
        COUNT(DISTINCT CASE WHEN column = 'progress' AND value = 'Red' THEN value END) AS RED,
        COUNT(DISTINCT CASE WHEN column = 'progress' THEN value END) AS PROGRESS_TOTAL
FROM    TABLE
GROUP BY Id