【发布时间】:2021-02-28 01:52:42
【问题描述】:
【问题讨论】:
-
请向我们展示您的尝试。请使用格式化文本而不是图像。
标签: sql sql-server pivot pivot-table aggregate-functions
【问题讨论】:
标签: sql sql-server pivot pivot-table aggregate-functions
您可以使用条件聚合:
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
【讨论】: