quote: Originally posted by anujpratap84 Thanks for reply..Can you please write a sample example query...Thanks in advance.quote: Originally posted by visakh16 you will need to use unpivot first and get them into rows. then apply pivot over them based on value of Month column------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Anuj Pratap Singh
see illustration belowCREATE TABLE #pivunpiv([Month] varchar(100), ALLTotal int,DEF int,TEN int,[ALL] int)insert #pivunpivSELECT '1', 45, 10, 15, 20 union allSELECT '2', 40, 5, 10, 25 union allSELECT 'Total', 85, 15, 35, 45select * from #pivunpivselect [cat] AS [Month],[1],[2],[Total] into #result from(select * from #pivunpiv punpivot ( Val FOR Cat IN (ALLTotal,DEF,TEN,[ALL]))u)tpivot (sum(Val) for [Month] IN ([1],[2],[Total]))pselect * from #resultdrop table #pivunpivdrop table #resultoutput-----------------------------------after unpivot-----------------------------------Month ALLTotal DEF TEN ALL1 45 10 15 202 40 5 10 25Total 85 15 35 45-----------------------------------after final pivot-----------------------------------Month 1 2 TotalALL 20 25 45ALLTotal 45 40 85DEF 10 5 15TEN 15 10 35 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |