SELECT *
     FROM (
           SELECT  DATO.FILA,DATO.LINEA,DATO.BULPEND,DATO.PEDPEND,DATO.CODAGE,DATO.TPEDIDOS,DATO.TBULTOS
            FROM (SELECT FLOOR(SECLABEL/1000) LINEA,(CASE WHEN SEX.CODAGE IS NULL THEN 'TODAS' ELSE SEX.CODAGE END) CODAGE,
                         COUNT((CASE WHEN PEC.STATUS<VDST.FPECFINALIZADO THEN PEC.CODPED ELSE NULL END)) PEDPEND,COUNT((CASE WHEN BUC.STATUS<VDST.FBUCFINALIZADO THEN BUC.CODBULTO ELSE NULL END)) BULPEND,
                         COUNT(DISTINCT PEC.CODPED) TPEDIDOS,COUNT(DISTINCT BUC.CODBULTO) TBULTOS,
                         DECODE(SEX.CODAGE,NULL,10,RANK() OVER (PARTITION BY FLOOR(SECLABEL/1000) ORDER BY SEX.CODAGE)) FILA
                   FROM VDSERIEEXP SEX,VDPEDIDOCAB PEC,VDBULTOCAB BUC
                   WHERE SEX.CODSERIEEXP=PEC.CODSERIEEXP AND BUC.CODPED=PEC.CODPED AND BUC.CODDIV=PEC.CODDIV AND BUC.ANOPED=PEC.ANOPED AND BUC.SEQPED=PEC.SEQPED AND
                         PEC.CODSERIEPREP=(CASE WHEN :MISERIEPREP='TODAS' THEN PEC.CODSERIEPREP ELSE TO_NUMBER(:MISERIEPREP) END)
                   GROUP BY FLOOR(SECLABEL/1000),ROLLUP (SEX.CODAGE)
                 ) DATO
          )
      PIVOT (sum(bulpend) AS BP,SUM(PEDPEND) AS PP,MAX(CODAGE) AS SP,SUM(TPEDIDOS) AS PT,SUM(TBULTOS) AS BT FOR (LINEA) IN (1 AS LIN1,2 AS LIN2,3 AS LIN3,4 AS LIN4,5 AS LIN5,6 AS LIN6,7 AS LIN7,8 AS LIN8,9 AS LIN9,10 AS LIN10)
);

SELECT FILA,LIN1_SE,LIN2_SE,LIN3_SE,LIN4_SE,LIN5_SE,LIN6_SE,LIN7_SE,LIN8_SE,LIN9_SE,LIN10_SE
FROM (SELECT FILA,LINEA,BULPEND FROM KK1)
PIVOT (sum(bulpend) AS SE FOR (LINEA) IN (1 AS LIN1,2 AS LIN2,3 AS LIN3,4 AS LIN4,5 AS LIN5,6 AS LIN6,7 AS LIN7,8 AS LIN8,9 AS LIN9,10 AS LIN10)
);

SELECT pt.*
   FROM   (SELECT customer_id,product_code, quantity
                  FROM   pivot_test) 
                 PIVOT  (SUM(quantity) AS sum_quantity FOR (product_code) IN ('A' AS a, 'B' AS b, 'C' AS c)) pt;

SELECT * FROM KK1;


DROP TABLE KK1;

CREATE TABLE KK1 AS
SELECT DATO.LINEA,DATO.FILA, DATO.BULPEND
FROM (SELECT FLOOR(SECLABEL/1000) LINEA,(CASE WHEN SEP.CODSERIEPREP IS NULL THEN 'TODAS' ELSE TO_CHAR(SEP.CODSERIEPREP) END) SERIEPREP,
            COUNT((CASE WHEN PEC.STATUS<VDST.FPECFINALIZADO THEN PEC.CODPED ELSE NULL END)) PEDPEND,COUNT((CASE WHEN BUC.STATUS<VDST.FBUCFINALIZADO THEN BUC.CODBULTO ELSE NULL END)) BULPEND,
            COUNT(DISTINCT PEC.CODPED) TPEDIDOS,COUNT(DISTINCT BUC.CODBULTO) TBULTOS,
            DECODE(SEP.CODSERIEPREP,NULL,10,RANK() OVER (PARTITION BY FLOOR(SECLABEL/1000) ORDER BY SEP.CODSERIEPREP)) FILA
FROM VDSERIEPREP SEP,VDPEDIDOCAB PEC,VDBULTOCAB BUC
WHERE SEP.CODSERIEPREP=PEC.CODSERIEPREP AND BUC.CODPED=PEC.CODPED AND BUC.CODDIV=PEC.CODDIV AND BUC.ANOPED=PEC.ANOPED AND BUC.SEQPED=PEC.SEQPED
GROUP BY FLOOR(SECLABEL/1000),ROLLUP (SEP.CODSERIEPREP)) DATO;

CREATE TABLE pivot_test (
  id            NUMBER,
  customer_id   NUMBER,
  product_code  VARCHAR2(5),
  quantity      NUMBER
);

INSERT INTO pivot_test VALUES (1, 1, 'A', 10);
INSERT INTO pivot_test VALUES (2, 1, 'B', 20);
INSERT INTO pivot_test VALUES (3, 1, 'C', 30);
INSERT INTO pivot_test VALUES (4, 2, 'A', 40);
INSERT INTO pivot_test VALUES (5, 2, 'C', 50);
INSERT INTO pivot_test VALUES (6, 3, 'A', 60);
INSERT INTO pivot_test VALUES (7, 3, 'B', 70);
INSERT INTO pivot_test VALUES (8, 3, 'C', 80);
INSERT INTO pivot_test VALUES (9, 3, 'D', 90);
INSERT INTO pivot_test VALUES (10, 4, 'A', 100);
COMMIT;
