I have created a pivot dynamically but i also want to add a sum on qtt (is integer) that i cannot do it.If i put a sum after 'SELECT [description],' + @cols + ' , it will say qtt column not found, if i group by after "x" and before pivot is still does not work.
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME([date])
group by [date]
order by [date]
FOR XML PATH(''), TYPE
set @query = 'SELECT [description],' + @cols + '
-- ,sum(a.qtt) as total
FROM Table_1 a
group by [date],[description],qtt
for [date] in (' + @cols + ')
) p '
execute sp_executesql @query;