I'm trying to produce a crosstab to move rows into columns using CASE statements. Trying to stay away from PIVOT because there are multiple columns I want to move and it looks like PIVOT will be too complex.
Tables: Person, Schedule
Schedule table has multiple records for one Person. I've got joins to other tables as well. What I'm looking to do is crosstab the rows into columns for the Schedule table. So instead of:
George sched1
George sched2
Geroge sched3
I want:
George sched1 sched2 sched3
I'm using this:
and this is what I'm getting. Each record is still being evaluated and displayed with a NULL where it doesn't match the CASE:
Person Schd1 Schd2 Schd3
George Paid NULL NULL
George NULL Paid NULL
George NULL NULL Paid
Tables: Person, Schedule
Schedule table has multiple records for one Person. I've got joins to other tables as well. What I'm looking to do is crosstab the rows into columns for the Schedule table. So instead of:
George sched1
George sched2
Geroge sched3
I want:
George sched1 sched2 sched3
I'm using this:
Code:
,MAX(CASE Schedule.Sched WHEN '1' THEN
(CASE WHEN Schedule.Status = 'R' THEN 'Ready' WHEN Schedule.Status = 'C' THEN 'Canceled' WHEN Schedule.Status = 'P' THEN 'Paid' WHEN Schedule.Status = 'S' THEN 'Scheduled' END) END) as Sched1_SchedStatus
,MAX(CASE Schedule.Sched WHEN '2' THEN
(CASE WHEN Schedule.Status = 'R' THEN 'Ready' WHEN Schedule.Status = 'C' THEN 'Canceled' WHEN Schedule.Status = 'P' THEN 'Paid' WHEN Schedule.Status = 'S' THEN 'Scheduled' END) END) as Sched2_SchedStatus
,MAX(CASE Schedule.Sched WHEN '3' THEN
(CASE WHEN Schedule.Status = 'R' THEN 'Ready' WHEN Schedule.Status = 'C' THEN 'Canceled' WHEN Schedule.Status = 'P' THEN 'Paid' WHEN Schedule.Status = 'S' THEN 'Scheduled' END) END) as Sched3_SchedStatus
,MAX(CASE Schedule.Sched WHEN '4' THEN
(CASE WHEN Schedule.Status = 'R' THEN 'Ready' WHEN Schedule.Status = 'C' THEN 'Canceled' WHEN Schedule.Status = 'P' THEN 'Paid' WHEN Schedule.Status = 'S' THEN 'Scheduled' END) END) as Sched4_SchedStatus
and this is what I'm getting. Each record is still being evaluated and displayed with a NULL where it doesn't match the CASE:
Person Schd1 Schd2 Schd3
George Paid NULL NULL
George NULL Paid NULL
George NULL NULL Paid