Código SQL:
Ver originalCREATE TABLE #temp
(
campo VARCHAR(25)
)
INSERT INTO #temp VALUES ('procesado')
INSERT INTO #temp VALUES ('procesado')
INSERT INTO #temp VALUES ('procesado')
INSERT INTO #temp VALUES ('procesado')
INSERT INTO #temp VALUES ('cerrado')
INSERT INTO #temp VALUES ('cerrado')
INSERT INTO #temp VALUES ('argentina')
INSERT INTO #temp VALUES ('argentina')
INSERT INTO #temp VALUES ('argentina')
INSERT INTO #temp VALUES ('brasil')
INSERT INTO #temp VALUES ('brasil')
INSERT INTO #temp VALUES ('brasil')
INSERT INTO #temp VALUES ('brasil')
INSERT INTO #temp VALUES ('brasil')
INSERT INTO #temp VALUES ('españa')
INSERT INTO #temp VALUES ('españa')
INSERT INTO #temp VALUES ('españa')
INSERT INTO #temp VALUES ('españa')
INSERT INTO #temp VALUES ('mexico')
DROP TABLE #temp2
SELECT DISTINCT campo, IDENTITY(INT,1,1) AS rn INTO #temp2 FROM #temp
DECLARE @x INT
DECLARE @valor VARCHAR(200)
DECLARE @query nvarchar(MAX)
DECLARE @query2 nvarchar(MAX)
DECLARE @query3 nvarchar(MAX)
SET @x=1
SET @query='select '
SET @query2='select '
while @x<=(SELECT COUNT(*) FROM #temp2)
BEGIN
SELECT @valor=campo FROM #temp2 WHERE rn=@x
SET @query2=@query2 + 'sum(' + @valor + ') as ' + @valor + ','
SET @query=@query + 'case when campo=' + CHAR(39) + @valor + CHAR(39) + ' then sum(1) end as ' + @valor + ', ' + CHAR(13)
SET @x=@x+1
END
SET @query=SUBSTRING(@query,1,len(@query)-3)
SET @query=@query + ' from #temp group by campo'
SET @query2=SUBSTRING(@query2,1,len(@query2)-1)
SET @query2=@query2 + ' from (' + CHAR(13) + @query + ') as t1'
EXEC sp_executesql @query2
algo como eso :)