Transpose columns in SQL with PIVOT?

1

Someone can easily explain me an example with PIVOT to transpose columns by rows in sql.

I have several data represented as follows:

FECHA                ERROR1    ERROR2 
2017-01-01           12        23

And I would like to transpose them like this:

FECHA             TIPO DE ERROR      TOTAL
2017-01-01        ERROR1             12
2017-01-01        ERROR2             23

The code I have:

SELECT [1] as Fecha, [2] as TìpoError, [3] as Total
FROM
(SELECT ERROR1,ERROR2
    FROM Errores) AS SourceTable
PIVOT
(
AVG(ERROR1)
FOR ERROR1, ERROR2 IN ([1], [2], [3])
) AS TablaEjemplo;

I can not find an example that is simple to move forward with this query.

    
asked by Yoel Macia Delgado 17.08.2017 в 09:34
source

1 answer

2

I have solved it with UNPIVOT, just like that.

select u.Fecha, u.TipoError, u.Total
from errores s
unpivot
(
  total
  for TipoError in (ERROR1, ERROR2)
) u;
    
answered by 17.08.2017 в 10:33