I Have a table (CERTS) with the folling columns
ID, NAME, STATUS.I can can have 1 id with multiple types and status
example ID NAME STATUS
412541 CAE cERTIFIED
412541 CSE APPROVED
412541 CAP null
412541 CDA INACTIVE
Now i would like to represent all id's in on single row eample
ID status_cae status_cse status_cap status_cda
412541 certified approved null INACTIVE
so i create a pivot table
create table #temp
(id varchar (200) null,
status_cae varchar (200) null,
status_cse varchar (200) null,
status_cap varchar (200) null,
status_cda varchar (200) null,)
insert into #temp
SELECT ID,[1] AS status_cae, [2] AS status_cse, [3] AS status_cap, [4] AS status_cda
from
(select id,type,
row_number()over (partition BY id order by id) as rownum
FROM CERTS)a
pivot (max(type) from rownum in ([1] , [2], [3], [4]) as pvt
however when i pivot this it returns
ID status_cae status_cse status_cap statuse_cda
412541 certified INACTIVE approved null
what am i doing wrong in my pivot and how do i get it to pivot correctly, i tried to change to oder of the row number which works
however when i retrieve another ID the sort order is out of order again, Please assist