Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Transform row to column query

Author  Topic 

anujpratap84
Starting Member

45 Posts

Posted - 2011-09-29 : 05:02:28
Hi All,

I am trying to transform my row to column using PIVOT keyword but it's not working for me.
My table structure is like this:

Month ALLTotal DEF TEN ALL
1 45 10 15 20
2 40 5 10 25
Total 85 15 35 45

I want to transforn it like

Month 1 2 Total
ALLTotal 45 40 55
DEF 10 5 15
TEN 15 10 35
ALL 20 25 45

How i get the result in this format?
Thanks in advance..

Anuj Pratap Singh

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-09-29 : 05:07:44
Hi,

Refer below links it will definitely help you
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

http://shivasoft.in/blog/sql/sqlserver/sql-server-pivot-table-example/

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-29 : 08:18:32
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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

anujpratap84
Starting Member

45 Posts

Posted - 2011-09-29 : 08:43:07
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 MVP
http://visakhm.blogspot.com/





Anuj Pratap Singh
Go to Top of Page

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-09-29 : 10:38:56
hi ,

i think the following link contains what you want :

http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-29 : 12:50:16
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 MVP
http://visakhm.blogspot.com/





Anuj Pratap Singh


see illustration below

CREATE TABLE #pivunpiv
(
[Month] varchar(100),
ALLTotal int,
DEF int,
TEN int,
[ALL] int
)
insert #pivunpiv
SELECT '1', 45, 10, 15, 20 union all
SELECT '2', 40, 5, 10, 25 union all
SELECT 'Total', 85, 15, 35, 45

select * from #pivunpiv

select [cat] AS [Month],[1],[2],[Total] into #result
from
(
select * from #pivunpiv p
unpivot ( Val FOR Cat IN (ALLTotal,DEF,TEN,[ALL]))u

)t
pivot (sum(Val) for [Month] IN ([1],[2],[Total]))p

select * from #result
drop table #pivunpiv
drop table #result


output
-----------------------------------
after unpivot
-----------------------------------
Month ALLTotal DEF TEN ALL
1 45 10 15 20
2 40 5 10 25
Total 85 15 35 45

-----------------------------------
after final pivot
-----------------------------------
Month 1 2 Total
ALL 20 25 45
ALLTotal 45 40 85
DEF 10 5 15
TEN 15 10 35



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -