Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

pivoting multiple coulmn values into multiple columns dynamically

$
0
0

Hi,

i have found some topics related to 'pivot' in this forum. but my situation is a lit bit different.

I have a table like the below

createtable#temp(id int,code varchar(8),routingvarchar(50))

insertinto#temp(id,code,routing )

values(1,    'code1',     'route1')

,(1,'code11','route11')

,(2,  'code22',    'route22')

,(2,  'code222',   'route222')

,(2,  'code2333',  'route2333')

,(2,  'code2',     'route2')

,(3,  'code3',     'route3'),

(4,   'code4',     'route4'),

(4,   'code44',    'route44')

ID

CODE

ROUTE

1

code1

route1

1

code11

route11

2

code22

route22

2

code222

route222

2

code2333

route2333

2

code2

route2

3

code3

route3

4

code4

route4

4

code44

route44

And I need to display the table below format including column names. And code/routing columns can repeat not more than 10 times.

id

code

routing

code

routing

code

routing

code

routing

1

code1

route1

code11

route11

2

code2

route2

code22

route22

code222

route222

code2333

route2333

3

code3

route3

4

code4

route4

code44

route44

I am using sql server 2012.

i have benefited alot from this forum. a big thank you to the forum.

I would appreciate  your help.


Viewing all articles
Browse latest Browse all 23857

Trending Articles