Proof of concept, change table or column names as needed:create table #a (VISIT_ID int, ICD_CODE decimal(8,2))insert #a select 212, 85.25 union all select 212, 74.25 union all select 245, 87.36 union all select 245, 75.21;with a(visit_id, icd_code, rn) as (select visit_id, icd_code, row_number() over (partition by visit_id order by icd_code desc) rn from #a)select visit_id, [1] ICD1, [2] ICD2, [3] ICD3, [4] ICD4, [5] ICD5 from apivot (max(icd_code) for rn in ([1],[2],[3],[4],[5])) b