Each ID can have many repeated rows as showing the first table to the left, the requirement is to place 'child' records of the same ID on the same row and repeat the column headers as showing below in the table on the right.
I am trying to do this in SQL Server, here is my attempt at it:
if Object_id('tempdb..#temp1') is not null
Begin
drop table #temp1
End
create table #temp1 (
ID integer, FirstName varchar(50), LastName varchar(50)
)
insert into #temp1 values (25,'Abby','Mathews');
insert into #temp1 values (25,'Jennifer','Edwards');
insert into #temp1 values (26,'Peter','Williams');
insert into #temp1 values (27,'John','Jacobs');
insert into #temp1 values (27,'Mark','Scott');
Select * From #temp1;
With Qrt_CTE (ID, FirstName, LastName)
AS
(
SELECT ID, FirstName, LastName
FROM #temp1 AS BaseQry
)
SELECT ID, ColumnName, ColumnValue INTO #temp2
FROM Qrt_CTE
UNPIVOT
(
ColumnValue FOR ColumnName IN (FirstName, LastName)
) AS UnPivotExample
Select * From #temp2
How do I get these results done please?
Thank you so much in advance, appreciate any help.