SQLTeam.com | Weblogs | Forums

Rows to Column Group by parent_ID


#1

Hi,

How to Transpose Table below

select parent_id='1',FieldName='HotFixRelNumber',val=1112
union all
select '1','HotfixType',2
union all
select '2','HotFixRelNumber',1113
union all
select '2','HotfixType',4
union all
select '3','HotFixRelNumber',1114
union all
select '3','HotfixType',21
union all
select '4','HotFixRelNumber',1115
union all
select '4','HotfixType',256

parent_id FieldName val
1 HotFixRelNumber 1112
1 HotfixType 2
2 HotFixRelNumber 1113
2 HotfixType 4
3 HotFixRelNumber 1114
3 HotfixType 21
4 HotFixRelNumber 1115
4 HotfixType 256

to

parent_id HotFixNumber HotFixType
1 1112 2
2 1113 4
3 1114 21
4 1115 256

Without hardcoding the column name. Please advise.

Thank you.

Regards,
Micheale


#2

Found the solution:-

select * from (select parent_id,FieldName ,val
from testA
) as Ter
pivot (max(val) for FieldName in (....))as bro

Thank you.