I'm not sure if that is exactly what you want. But It gives your expected result. Note you can rename the columns as you please.
SET NOCOUNT ON
declare @t table(
cid int,
OverviewText varchar(15),
RAGStatusText varchar(15)
)
insert into @t values (153, 'Cost', 'Green');
insert into @t values (154, 'Requirements', 'Yellow');
insert into @t values (155, 'Schedule', 'NULL');
insert into @t values (156, 'Technical', 'NULL');
insert into @t values (157, 'Testing', 'NULL');
Select
[153|1] as [xyz],
[153|2],
[154|1],
[154|2],
[155|1],
[155|2],
[156|1],
[156|2],
[157|1],
[157|2]
FROM (
select CAST(cid as varchar) + '|1' type, OverviewText text from @t
Union
select CAST(cid as varchar) + '|2' type, RAGStatusText text from @t
) as SourceTable
Pivot(
min(text)
for type in ([153|1],[153|2], [154|1],[154|2], [155|1],[155|2], [156|1],[156|2], [157|1],[157|2])
) as PivotTable;
Best Answer
If you have the following table of data that you want to pivot:
And your current code is similar to:
See SQL Fiddle with Demo. The sample data will create
null
values.When you want to replace the
null
values you have to do it in the finalSELECT
list. So your code will need to be:See SQL Fiddle with Demo