对具有相同数据的多个列进行透视

Pivot on multiple columns with same data

我有一组数据

我怎样才能达到这样的效果

您可以使用条件聚合:

select targetchildid,
    max(case when name = 'Hepatitis B #1' then datereceived   end) as hepatitis_b1_date_received,
    max(case when name = 'Hepatitis B #1' then ajustedduedate end) as hepatitis_b1_adjusted_due_date,
    max(case when name = 'Hepatitis B #1' then isskipped      end) as hepatitis_b1_adjusted_is_skipped,
    max(case when name = 'Hepatitis B #2' then datereceived   end) as hepatitis_b2_date_received,
    max(case when name = 'Hepatitis B #2' then ajustedduedate end) as hepatitis_b2_adjusted_due_date,
    max(case when name = 'Hepatitis B #2' then isskipped      end) as hepatitis_b2_adjusted_is_skipped,
    max(case when name = 'Hepatitis B #3' then datereceived   end) as hepatitis_b3_date_received,
    max(case when name = 'Hepatitis B #3' then ajustedduedate end) as hepatitis_b3_adjusted_due_date,
    max(case when name = 'Hepatitis B #3' then isskipped      end) as hepatitis_b3_adjusted_is_skipped
from mytable
group by targetchildid