I posted my code to get the rows to column by using pivot, but I need to get the sum of each row after pivot.
BEGIN
SELECT @BranchName = BranchName
FROM @t
WHERE id = @i
SELECT @columns = (
SELECT DISTINCT ','+QUOTENAME([SubInventory])
FROM #MyTempTable
WHERE [BranchName] = @BranchName
FOR XML PATH('')
)
SELECT @sql = N'--
SELECT *
FROM (
SELECT [BranchID],
[SubInventory],
[Product],
[Stock]
FROM #MyTempTable
WHERE [BranchName] = ''' +@BranchName +'''
) as t
PIVOT (
MAX([Stock]) FOR [SubInventory] IN ('+STUFF(@columns,1,1,'')+')
) as pvt'
EXEC sp_executesql @sql
SET @i = @i+1
I attached the format how the result is returned, in this picture two tables are there with different Branch ID, so I need the sum of the column name after products
My required result is
Current Screen