SQL Server 2008 R2 usando PIVOT con columnas varchar que no funcionan
Estoy usando SQL Server 2008 R2, tengo esta tabla simple
Lo que estaba tratando de hacer es hacer una selección de esta tabla y obtener el siguiente resultado
x | 1 | 2 | 3
--+------------+-------------+------------
1 | first 1 | first 2 | first 3
2 | Second 1 | second 2 | second 3
Pensé que se puede hacer conPIVOT
No se mucho sobrePIVOT
Y todos mis resultados de búsqueda encontrados utilizando PIVOT conCount()
. SUM()
, AVG()
que no funcionará en mi mesa ya que estoy tratando dePIVOT
en unvarchar
columna
Pregunta ¿Estoy usando la función correcta? ¿O hay algo más que necesito saber para resolver este problema? Cualquier ayuda será apreciada.
Probé esto sin suerte
PIVOT(count(x) FOR value IN ([1],[2],[3]) )as total
PIVOT(count(y) FOR value IN ([1],[2],[3]) )as total // This one is the nearest
of what i wand but instead of the column value values i get 0
Aquí está la consulta si hay alguien para probarlo
CREATE TABLE #test (x int , y int , value Varchar(50))
INSERT INTO #test VALUES(1,51,'first 1')
INSERT INTO #test VALUES(1,52,'first 2')
INSERT INTO #test VALUES(1,53,'first 3')
INSERT INTO #test VALUES(2,51,'Second 1')
INSERT INTO #test VALUES(2,52,'Second 2')
INSERT INTO #test VALUES(2,53,'Second 3')
SELECT * FROM #test
PIVOT(count(y) FOR value IN ([1],[2],[3]) )as total
DROP TABLE #test