我正在尝试编写查询以将行转置为列。附件中的图像有一个我要转置的样本表和预期的输出。
感谢任何输入。
答案 0 :(得分:0)
选项1.条件聚合
select labid, subjectid,
max(case when timepoint='T0' then val1 end) T0_val1,
max(case when timepoint='T0' then val2 end) T0_val2,
max(case when timepoint='T1' then val1 end) T1_val1,
max(case when timepoint='T1' then val2 end) T1_val2
from input
group by labid, subjectid;
选项2。取消枢纽/枢纽
select * from
(
select labid, subjectid, timepoint+'_'+col as timepoint_col, val
from
(select labid, subjectid, timepoint, val1, val2
from input) as src
unpivot
(
val for col in (val1, val2)
) as unpiv) as x
pivot
(
max(val)
for timepoint_col in ([T0_val1],[T0_val2],[T1_val1],[T1_val2])
) as piv1