带有数据透视表和行总计的oracle查询

时间:2016-04-07 07:28:22

标签: oracle pivot

这是我的整个要求以及创建和插入命令。

    create table sample
    ( schedule_id varchar2(256),
    schedule_details varchar2(256),
    pod_name varchar2(256),
    milestone varchar2(256),
    metric_column varchar2(256),
    metric_value varchar2(4000));

    insert into sample values ('23456','details','ABCD','M1','status','DONE');
    insert into sample values ('123456','details','ABCD','M1','progress','GREEN');
    insert into sample values ('123456','details','ABCD','M1','last_reported','2016-03-18 04:18:56');
    insert into sample values ('123456','det','ABCD','M2','status','DONE');
    insert into sample values ('123456','det','ABCD','M2','progress','RED');
    insert into sample values ('123456','det','ABCD','M2','last_reported','2016-03-19 04:18:56');
    insert into sample values ('123456','det','ABCD','M3','status','SKIP');
    insert into sample values ('123456','det','ABCD','M3','progress','YELLOW');
    insert into sample values ('123456','det','ABCD','M3','last_reported','2016-03-20 04:18:56');
    insert into sample values ('123456','det''EFGH','M1','status','DONE');
    insert into sample values ('123456','det','EFGH','M1','progress','GREEN');
    insert into sample values ('123456','det','EFGH','M1','last_reported','2016-03-10 04:18:56');
    insert into sample values ('123456','det','EFGH','M2','status','DONE');
    insert into sample values ('123456','det','EFGH','M2','progress','RED');
    insert into sample values ('123456','det','EFGH','M2','last_reported','2016-03-12 04:18:56');
    insert into sample values ('123456','det','EFGH','M3','status','SKIP');
    insert into sample values ('123456','det','EFGH','M3','progress','YELLOW');
    insert into sample values ('123456','det','EFGH','M3','last_reported','2016-03-11 04:18:56');
    insert into sample values ('7890','det','ABCD','M1','status','DONE');
    insert into sample values ('7890','det','ABCD','M1','progress','GREEN');
    insert into sample values ('7890','det','ABCD','M1','last_reported','2016-04-18 04:18:56');
    insert into sample values ('7890','det','ABCD','M2','status','DONE');
    insert into sample values ('7890','det','ABCD','M2','progress','RED');
    insert into sample values ('7890','det','ABCD','M2','last_reported','2016-04-19 04:18:56');
    insert into sample values ('7890','det','ABCD','M3','status','FAILED');
    insert into sample values ('7890','det','ABCD','M3','progress','GREEN');
    insert into sample values ('7890','det','ABCD','M3','last_reported','2016-04-20 04:18:56');
    insert into sample values ('7890','det','PQRS','M1','status','DONE');
    insert into sample values ('7890','det','PQRS','M1','progress','GREEN');
    insert into sample values ('7890','det','PQRS','M1','last_reported','2016-04-10 04:18:56');
    insert into sample values ('7890','det','PQRS','M2','status','DONE');
    insert into sample values ('7890','det','PQRS','M2','progress','RED');
    insert into sample values ('7890','det','PQRS','M2','last_reported','2016-04-11 04:18:56');
    insert into sample values ('7890','det','PQRS','M3','status','SKIP');
    insert into sample values ('7890','det','PQRS','M3','progress','GREEN');
    insert into sample values ('7890','det','PQRS','M3','last_reported','2016-04-12 04:18:56');

我的输出是

    SCHEDULE_ID      |   STATUS_DONE   |  STATUS_SKIP    | STATUS_FAILED  | STATUS_TOTAL      |  PROGRESS_RED  | PROGRESS_GREEN   | PROGRESS_YELLOW    | PROGRESS_TOTAL
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    123456           | 1               | 1               | 0               | 2                 | 1               |  0               |  1                 | 2
    7890             |  0              | 2               | 0               |  2                 | 0               | 1                | 1                  |  2

查询需要为每个schedule_id获取不同数量的pod_name。对于每个pod,它需要采用里程碑的metric_column->状态和metric_column->进度,其中包含最新的metric_column-> last_reported。因此,STATUS_DONE的值是具有最新里程碑(基于last_reported)的POD_NAME的数量,状态为DONE。

例如对于时间表ID 123456,pod ABCD,需要采用里程碑M3的状态和进度,因为M3与M1和M2相比具有最新的last_report。

我的查询具有不同的pod名称,而没有考虑最新的里程碑。

    select *
    from (SELECT  SCHEDULE_ID,SCHEDULE_DETAILS,POD_NAME,
    CASE WHEN GROUPING(value) = 0
     THEN value
     ELSE CASE WHEN METRIC_COLUMN='status'
          THEN 'STATUS_TOTAL'
          ELSE 'PROGRESS_TOTAL'
          END
     END value
    FROM sample
    WHERE metric_column IN ('status','progress' )
    GROUP BY SCHEDULE_ID,SCHEDULE_DETAILS,POD_NAME, METRIC_COLUMN, 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"));

0 个答案:

没有答案