一、行转列:
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,...TitleCodefrom T_Optiongroup by TitleCode
二、列转行:
select * from(select ExamRecordId,TitleCode,max(oa) OptionKey,max(A) OptionCont from V_Titlegroup by ExamRecordId,TitleCodeunion allselect ExamRecordId,TitleCode,max(ob) OptionKey,max(B) OptionCont from V_Titlegroup by ExamRecordId,TitleCodeunion allselect ExamRecordId,TitleCode,max(oc) OptionKey,max(C) OptionCont from V_Titlewhere C is not nullgroup by ExamRecordId,TitleCodeunion allselect ExamRecordId,TitleCode,max(od) OptionKey,max(D) OptionCont from V_Titlewhere D is not nullgroup by ExamRecordId,TitleCode) Taborder by TitleCode,ExamRecordId
看看大佬的文章昂
如果觉得《mssql行转列 列转行》对你有帮助,请点赞、收藏,并留下你的观点哦!