Hi.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.Any thoughts?DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)select @cols = STUFF((SELECT ',' + QUOTENAME([date]) from Table_1 group by [date] order by [date] FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')set @query = 'SELECT [description],' + @cols + ' from ( SELECT a.[description], a.[date], a.qtt -- ,sum(a.qtt) as total FROM Table_1 a group by [date],[description],qtt ) x pivot ( max(qtt) for [date] in (' + @cols + ') ) p ' execute sp_executesql @query;