日期:2014-05-18 浏览次数:20399 次
declare @sql varchar(8000) set @sql='' select @sql=@sql+',['+类型+']=max(case 类型 when '+类型+' then 时差 else ''0'' end)' from 表 gruop by 类型 set @sql='select 姓名'+@sql+' from 表 where starttime>''2007-05-01'' endtime<''2007-05-02'' group by 姓名' exec(@sql)
------解决方案--------------------
select
姓名,
项目=sum(case 类型 when '项目' then cast(left(时差,1) as int)*60 + cast(right(时差,2) as int ) else 0 end),
学习=sum(case 类型 when '学习' then cast(left(时差,1) as int)*60 + cast(right(时差,2) as int ) else 0 end),
无关=sum(case 类型 when '无关' then cast(left(时差,1) as int)*60 + cast(right(时差,2) as int ) else 0 end)
from tab1(表名) group by 姓名
cast(left(时差,1) as int)*60 + cast(right(时差,2) as int ) 是把时差转化为了分钟:例如1:01转化为61分钟,求和后你再转化回来,我没有时间