示例:
原本是同一个学生的2条记录,把它行转列后:
select studentnid ,max(case StudentSourceTypeNid when'1' then SourcePrimaryKey else '0' end) vipSourcePrimaryKey, max(case StudentSourceTypeNid when'1' then SourceDate else null end) vipSourceDate, max(case StudentSourceTypeNid when'2' then SourcePrimaryKey else '0' end) stmsSourcePrimaryKey, max(case StudentSourceTypeNid when'2' then SourceDate else null end) stmsSourceDate, max(case StudentSourceTypeNid when'3' then SourcePrimaryKey else '0' end) webSourcePrimaryKey, max(case StudentSourceTypeNid when'3' then SourceDate else null end) webSourceDate from ( select student.*, stuSource.SourcePrimaryKey, stuSource.SourceDate, stuSource.StudentSourceTypeNid from dbo.MEM_Student as student inner join dbo.MEM_StudentSource as stuSource on student.studentnid=stuSource.studentnid ) as tb where studentnid=@studentnid group by studentnid,studentname,sex
成了这样的效果:
以上就是代码了,我懂的。