Row and column total in dynamic pivot
In SQL Server 2008, I have a table (tblStock) with 3 columns: PartCode (NVARCHAR (50)) StockQty (INT) Location (NVARCHAR(50)) some example data below: PartCode StockQty Location ......... ......... ......... A 10 WHs-A B 22 WHs-A A 1 WHs-B C 20 WHs-A D 39 WHs-F E 3 WHs-D F 7 WHs-A A 9 WHs-C D 2 WHs-A F 54 WHs-E How to create procedure to get the result as below? PartCode WHs-A WHs-B WHs-C WHs-D WHs-E WHs-F Total ........ ..... ..... ..... ...... ..... ..... ..... A 10 1 9 0 0 0 20 B 22 0 0 0 0 0 22 C 20 0 0 0 0 0 20 D 2 0 0 0 0 39 41 E 0 0 0 3 0 0 3 F 7 0 0 0 54 0 61 Total 61 1 9 3 54 39 167