【问题标题】:Pivot on multiple columns with same data透视具有相同数据的多个列
【发布时间】:2021-02-28 01:52:42
【问题描述】:

我有一组数据

我怎样才能实现这样的目标

【问题讨论】:

  • 请向我们展示您的尝试。请使用格式化文本而不是图像。

标签: sql sql-server pivot pivot-table aggregate-functions


【解决方案1】:

您可以使用条件聚合:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多