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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Inverting rows into columns

Author  Topic 

bi89405
Starting Member

35 Posts

Posted - 2011-11-29 : 16:59:13
I have the following dataset.

AcctID input_name data
A1225 WOFFAR Off
A1225 CLOSEMT Off
A1225 CURRSTAT Off
A1225 FORMSTAT Off
A1225 FORMCLLTR Yes
A1225 WOFFUB Off

I would like to get the following output.

AcctID WOFFAR CLOSEMT CURRSTAT FORMSTAT FORMCLLTR WOFFUB
A1225 Off Off Off Off Yes Off

Any way to do that using SQL 2005?

TIA,
Zack H.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-11-29 : 17:38:11
You'll want to use the PIVOT operator with the MAX aggregate function, examples can be found here: http://msdn.microsoft.com/en-us/library/ms177410.aspx
Go to Top of Page

sql-programmers
Posting Yak Master

190 Posts

Posted - 2011-11-29 : 21:51:18
I have a blog article to maybe assist you with your question,
http://www.sql-programmers.com/Blog/tabid/153/EntryId/6/Using-PIVOT-and-UNPIVOT.aspx

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-30 : 01:30:47
i hope your header column values will be static else you've to use dynamic sql

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

Go to Top of Page
   

- Advertisement -