日期:2014-05-18 浏览次数:20543 次
select name, max(case px when 1 then Time else 0 end) as time1, max(case px when 2 then Time else 0 end) as time2, max(case px when 3 then Time else 0 end) as time3, max(case px when 4 then Time else 0 end) as time4 from (select px=row_number()over(order by getdate()),* from tb)t group by name
------解决方案--------------------
;with cte as( select *,row=ROW_NUMBER()over(PARTITION by [name] order by [time]) from Table1 ) select [name], max(case row when 1 then [time] else 0 end) as time1, max(case row when 2 then [time] else 0 end) as time2, max(case row when 3 then [time] else 0 end) as time3, max(case row when 4 then [time] else 0 end) as time4 from cte group by [name] name time1 time2 time3 time4 -------------------------------------------------- ----------- ----------- ----------- ----------- 张三 745 945 1210 1520 (1 行受影响)