Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 以具有相同数据的多列为轴心_Sql_Sql Server_Pivot_Pivot Table_Aggregate Functions - Fatal编程技术网

Sql 以具有相同数据的多列为轴心

Sql 以具有相同数据的多列为轴心,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

我有一套数据

我怎样才能做到这一点


您可以使用条件聚合:

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

请让我们看看你的尝试。请使用格式化文本而不是图像。