I am using SQL Server 2012, i need help in getting the following output. My table structure is as follows:
declare @TestTable table (MaterialCode char(5), ItemCode char(5), ItemName varchar(50))
insert into @TestTable values ('AA-01', 'A0001', 'iPhone')
insert into @TestTable values ('AA-02', 'A0001', 'iPad')
insert into @TestTable values ('AA-03', 'A0001', 'iPod')
insert into @TestTable values ('AA-01', 'B0001', 'Galaxy Tab')
insert into @TestTable values ('AA-02', 'B0001', 'Galaxy Note')
insert into @TestTable values ('AA-01', 'C0001', 'Nokia Lumnia')
insert into @TestTable values ('AA-02', 'C0001', 'Motorola')
insert into @TestTable values ('AA-03', 'C0001', 'Samsung S3')
insert into @TestTable values ('AA-04', 'C0001', 'Sony')
--select * from @TestTable
select MaterialCode, ItemCode as [A_ItemCode], ItemName as [A_ItemName]
from @TestTable where ItemCode='A0001'
select MaterialCode, ItemCode as [B_ItemCode], ItemName as [B_ItemName]
from @TestTable where ItemCode='B0001'
select MaterialCode, ItemCode as [C_ItemCode], ItemName as [C_ItemName]
from @TestTable where ItemCode='C0001'
And the output i need should produced from the above three select statements, which should be as follows:

As you can see, when there is no record, NULLs are displayed there. Can anyone help me getting this output. TIA.
EDIT
@JohnLBevan, I tried your pivot approach and when I have another record which is has ItemCode = D0001
insert into @TestTable values ('AA-05', 'D0001', 'Test1')
now even that record is being displayed as
AA-05 NULL NULL NULL NULL NULL NULL
How to avoid these type of records.
Not using a pivot, but this gives you the desired results:
Here’s an alternate which uses a pivot, though this feels messier:
EDIT
If you want something a little more flexible, here’s a dynamic version: