Ver Mensaje Individual
  #5 (permalink)  
Antiguo 19/06/2014, 13:40
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Duda para obtener información por PIVOT

sencillo:

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2.     (
  3.     num INT,
  4.     clase01 BIGINT,
  5.     clase02 INT,
  6.     clase03 INT,
  7.     clase04 INT,
  8.     clase05 INT,
  9.     clase06 INT,
  10.     clase07 SMALLINT
  11.     )
  12.        
  13.        INSERT INTO #temp VALUES (1      ,      1000    ,      2000    ,     3000    ,      4000    ,      5000    ,      6000,45)
  14.        INSERT INTO #temp VALUES (2      ,      7000    ,      8000    ,     9000    ,     10000   ,    11000    ,    12000,55)
  15.      
  16.      
  17.     SELECT tabla1.numero,tabla1.[1],tabla2.[2] FROM(
  18.     SELECT numero,[1]
  19.     FROM
  20.     (
  21.         SELECT CONVERT(BIGINT,clase01) AS clase01,CONVERT(BIGINT,clase02) AS clase02,
  22.     CONVERT(BIGINT,clase03) AS clase03,CONVERT(BIGINT,clase04) AS clase04,
  23.     CONVERT(BIGINT,clase05) AS clase05,CONVERT(BIGINT,clase06) AS clase06,CONVERT(BIGINT,clase07) AS clase07
  24.    
  25.      FROM #temp WHERE num=1
  26.     ) AS t1
  27.     unpivot
  28.     (
  29.     [1] FOR numero IN (clase01,clase02,clase03,clase04,clase05,clase06,clase07)
  30.     ) AS t2
  31.     ) AS tabla1
  32.     LEFT JOIN
  33.      
  34.     (
  35.     SELECT numero,[2]
  36.     FROM
  37.     (
  38.     SELECT CONVERT(BIGINT,clase01) AS clase01,CONVERT(BIGINT,clase02) AS clase02,
  39.     CONVERT(BIGINT,clase03) AS clase03,CONVERT(BIGINT,clase04) AS clase04,
  40.     CONVERT(BIGINT,clase05) AS clase05,CONVERT(BIGINT,clase06) AS clase06,CONVERT(BIGINT,clase07) AS clase07
  41.    
  42.      FROM #temp WHERE num=2
  43.     ) AS t1
  44.     unpivot
  45.     (
  46.     [2] FOR numero IN (clase01 ,clase02,clase03,clase04,clase05,clase06,clase07)
  47.     ) AS t2) AS tabla2 ON (tabla1.numero=tabla2.numero)

saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me