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 2008 Forums
 Transact-SQL (2008)
 Transpose data

Author  Topic 

ams006
Yak Posting Veteran

55 Posts

Posted - 2014-11-12 : 07:29:17
Hi all,

I have the below data extract:

ACCOUNT CODE
C217831A34FA436AA55D30A1CB9141EA WP_2
C217831A34FA436AA55D30A1CB9141EA CT_3
C217831A34FA436AA55D30A1CB9141EA DA_128
C217831A34FA436AA55D30A1CB9141EA NM_1

I need to transpose the data so instead of having 4 rows, I have only 1 row with four columns i.e.:

ACCOUNT CODE CODE1 CODE2 CODE3 CODE4
C217831A34FA436AA55D30A1CB9141EA WP_2 CT_3 DA_128 NM_1

Many thanks

sunder.bugatha
Yak Posting Veteran

66 Posts

Posted - 2014-11-13 : 09:38:15
/ SELECT MAX(ACCOUNT)ACCOUNT,
MAX(CASE WHEN CODE = 'WP_2' THEN 'WP_2'END)CODE1,
MAX(CASE WHEN CODE='CT_3' THEN 'CT_3' END)CODE2,
MAX(CASE WHEN CODE='DA_128' THEN 'DA_128' END)CODE3,
MAX(CASE WHEN CODE='NM_1' THEN 'NM_1' END)CODE4
FROM test1

Hema Sunder
Go to Top of Page
   

- Advertisement -