一、行转列:
select
max(case OptionKey when 'A' then OptionCont else null end) A,
max(case OptionKey when 'B' then OptionCont else null end) B,
max(case OptionKey when 'C' then OptionCont else null end) C,
max(case OptionKey when 'D' then OptionCont else null end) D,
...
TitleCode
from T_Option
group by TitleCode
二、列转行:
select * from(
select ExamRecordId,TitleCode,max(oa) OptionKey,max(A) OptionCont from V_Title
group by ExamRecordId,TitleCode
union all
select ExamRecordId,TitleCode,max(ob) OptionKey,max(B) OptionCont from V_Title
group by ExamRecordId,TitleCode
union all
select ExamRecordId,TitleCode,max(oc) OptionKey,max(C) OptionCont from V_Title
where C is not null
group by ExamRecordId,TitleCode
union all
select ExamRecordId,TitleCode,max(od) OptionKey,max(D) OptionCont from V_Title
where D is not null
group by ExamRecordId,TitleCode
) Tab
order by TitleCode,ExamRecordId