Ver Mensaje Individual
  #2 (permalink)  
Antiguo 18/11/2011, 18:49
Avatar de matanga
matanga
 
Fecha de Ingreso: octubre-2007
Ubicación: España
Mensajes: 1.091
Antigüedad: 16 años, 6 meses
Puntos: 85
Respuesta: Pivotar Consulta en Oracle 10g

Con las siguientes consultas puedes hacer de forma simple un pivot fila-columna o columna-fila.

Código:
SQL> create table t1 (id number(8), tipo varchar2(10))
  2  /
Table created.
SQL> insert into t1 values (1,'tipo1')
  2  /
1 row created.
SQL> insert into t1 values (2,'tipo2')
  2  /
1 row created.
SQL> insert into t1 values (3,'tipo3')
  2  /
1 row created.
SQL> insert into t1 values (4,'tipo4')
  2  /
1 row created.
SQL> select max(case when tipo = 'tipo1' then tipo end) tipo1
  2        ,max(case when tipo = 'tipo2' then tipo end) tipo2
  3        ,max(case when tipo = 'tipo3' then tipo end) tipo3
  4        ,max(case when tipo = 'tipo4' then tipo end) tipo4
  5  from t1
  6  /
TIPO1      TIPO2      TIPO3      TIPO4
---------- ---------- ---------- ----------
tipo1      tipo2      tipo3      tipo4
Código:
SQL> create table t1 (
  2   id number(8),
  3   tipo1 varchar2(10),
  4   tipo2 varchar2(10),
  5   tipo3 varchar2(10),
  6   tipo4 varchar2(10)
  7  )
  8  /
Table created.
SQL> insert into t1 values (1,'tipo1','tipo2','tipo3','tipo4')
  2  /
1 row created.
SQL> select id, tipo
  2  from (
  3   select id, tipo1 as tipo from t1
  4   union
  5   select id, tipo2 as tipo from t1
  6   union
  7   select id, tipo3 as tipo from t1
  8   union
  9   select id, tipo4 as tipo from t1
 10  )
 11  /
        ID TIPO
---------- ----------
         1 tipo1
         1 tipo2
         1 tipo3
         1 tipo4
Estos ejemplos sirven si conoces de antemano las columnas o filas involucradas en el pivot, en caso contrario, donde las columnas o filas son variables, puedes generar la consulta utilizando SQL dinámico.

Saludos