Q:

TSQL Transpose FROM Rows to Colums No Aggregration

I have the following style data from a csv file I have imported
Appt_ID,Sched_ID,Person_ID,Order_Field_Name,Order_Field Value
10001,2002,123456,Name,Fred Jones
10001,2002,123456,Address,"123 ABC St, Seasame Town"
10001,2002,123456,Appt_type,Consultation
10001,2002,123456,Sched_Start_time,12:00PM
10001,2002,123456,Sched_End_time,02:00PM
10001,2002,123456,Appt_With,Larry Smithe,
10002,2000,987654,Name,Mary Mags
10002,2000,987654,Address,"258 Street Ave, Neverland"
10002,2000,987654,Appt_type,Procedure
10002,2000,987654,Sched_Start_time,10:00AM
10002,2000,987654,Sched_End_time,4:00PM
10002,2000,987654,Appt_With,Larry Smithe

I need to transform it to look like this. I have tried using a pivot function but not coming out right.
Appt_ID,Sched_ID,Person_ID,Name,Address,Appt_type,Sched_Start_time,Sched_End_time,Appt_With
10001,2002,123456,Fred Jones,"123 ABC St, Seasame Town",Consultation,12:00PM,02:00PM,Larry Smithe
10002,2000,987654,Mary Mags,"258 Street Ave, Neverland",Procedure,10:00AM,4:00PM,Larry Smithe

Any help would be great.
Thanks in advance.

A:

You can do this with conditional aggregation:
select appt_id, sched_id, person_id,
       max(case when order_field = 'Name' then order_value end) as name,
       max(case when order_field = 'Address' then order_value end) as address,
       max(case when order_field = 'Appt_type' then order_value end) as appt_type,
       . . .
from table t
group by appt_id, sched_id, person_id;

