Declare @strSQL varchar(max)
DECLARE @cols NVARCHAR(2000)
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + convert(varchar, yourdatecolumn)
FROM yourtable
ORDER BY '],[' + convert(varchar, yourdatecolumn)
FOR XML PATH('')
), 1, 2, '') + ']'
set @strSQL = 'select ' + @cols +
' from (
select column1, column2, yourdatecolumn from yourtable) o
pivot (count(column1) for yourdatecolumn in (' + @cols + ')) p'
exec(@strSQL)
|