COLUMNAS MÚLTIPLES DE PIVOTE DE TSQL


Tengo la siguiente tabla pero no estoy seguro de si es posible pivotar esto y conservar todas las etiquetas.

RATIO               RESULT  SCORE   GRADE
Current Ratio       1.294   60      Good
Gearing Ratio       0.3384  70      Good
Performance Ratio   0.0427  50      Satisfactory
TOTAL               NULL    180     Good

Admitiré que no soy muy bueno con el uso de pivotes, así que después de varios intentos que resultan en esta salida:

SELECT 'RESULT' AS 'Ratio'
  ,[Current Ratio] AS 'Current Ratio'
  ,[Gearing Ratio] AS 'Gearing Ratio'
  ,[Performance Ratio] AS 'Performance Ratio'
  ,[TOTAL] AS 'TOTAL'
FROM
(
  SELECT RATIO, RESULT 
  FROM GRAND_TOTALS
) AS SREC
PIVOT 
(
  MAX(RESULT) 
  FOR RATIO IN ([Current Ratio],[Gearing Ratio], [Performance Ratio], [TOTAL])
) AS PVT

Esto da el resultado:

Ratio   Current Ratio   Gearing Ratio   Performance Ratio
Result  1.294       0.3384      0.0427

Admitiré que me siento muy perplejo sobre qué hacer a continuación para producir el resultado que necesito que es:

Ratio   Current Ratio   Gearing Ratio   Performance Ratio   TOTAL
Result     1.294        0.3384             0.0427            NULL
Score      60             70                50               180
Grade      Good          Good         Satisfactory           Good
Author: Taryn, 2013-10-25

1 answers

Dado que desea pivotar varias columnas de datos, primero sugeriría desempolvar la result, score y grade columnas para que no tenga varias columnas, pero tendrá varias filas.

Dependiendo de su versión de SQL Server puede utilizar la función UNPIVOT o CROSS APPLY. La sintaxis para desempolvar los datos será similar a:

select ratio, col, value
from GRAND_TOTALS
cross apply
(
  select 'result', cast(result as varchar(10)) union all
  select 'score', cast(score as varchar(10)) union all
  select 'grade', grade
) c(col, value)

Ver Violín SQL con Demo. Una vez que los datos han sido despivotados, puede aplicar la función PIVOT:

select ratio = col,
  [current ratio], [gearing ratio], [performance ratio], total
from
(
  select ratio, col, value
  from GRAND_TOTALS
  cross apply
  (
    select 'result', cast(result as varchar(10)) union all
    select 'score', cast(score as varchar(10)) union all
    select 'grade', grade
  ) c(col, value)
) d
pivot
(
  max(value)
  for ratio in ([current ratio], [gearing ratio], [performance ratio], total)
) piv;

Véase SQL Fiddle con Demo . Esto te dará el resultado:

|  RATIO | CURRENT RATIO | GEARING RATIO | PERFORMANCE RATIO |     TOTAL |
|--------|---------------|---------------|-------------------|-----------|
|  grade |          Good |          Good |      Satisfactory |      Good |
| result |       1.29400 |       0.33840 |           0.04270 |    (null) |
|  score |      60.00000 |      70.00000 |          50.00000 | 180.00000 |
 41
Author: Taryn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-25 13:34:14