代码如下:
create proc [dbo].[getTimeStatusFromTaskLog] as --首先将临时表清空 delete from [TEMP_TASK_LOG_STEP1] delete from [TEMP_TASK_LOG_STEP2] --先将所有的任务的前三条外呼记录行转换成列 insert into [TEMP_TASK_LOG_STEP1] select taskid, case when CALL_SEQ='2' then ENDTIME end as 'firstTime', case when CALL_SEQ='2' then operflag end as 'firstStatus', case when CALL_SEQ='3' then ENDTIME end as 'secondTime', case when CALL_SEQ='3' then operflag end as 'secondStatus', case when CALL_SEQ='4' then ENDTIME end as 'thirdTime', case when CALL_SEQ='4' then operflag end as 'thirdStatus' from T_QUESTIONNAIRE_TASK_LOG --将三行合并成一行,往临时表中填数据,让外面的表左外连接用 insert into [TEMP_TASK_LOG_STEP2] select taskid,MAX(firstTime) as 'firsttime',MAX(firstStatus) as 'firstStatus', MAX(secondTime) as 'secondtime',MAX(secondStatus) as 'secondStatus', MAX(thirdTime) as 'thirdtime',MAX(thirdStatus) as 'thirdStatus' from [TEMP_TASK_LOG_STEP1] group by taskid
一般行转列都会找到一些固定的列。比如此例子中的CALL_SEQ的值,就是2、3、4的固定值。
refurl:http://www.cnblogs.com/kerrycode/archive/2010/07/28/1786547.html