如何将行转换为列

时间:2015-11-27 13:30:36

标签: sql

我有一张桌子

GetkingResult

我想这样做

empname LeaveFrom Leave To
Peter   11/05/2015 11/08/2015
peter   09/06/2015  09/08/2015

1 个答案:

答案 0 :(得分:2)

简单的解决方案,自我加入:

select t1.empname, t1.LeaveFrom, t1.LeaveTo, t2.LeaveFrom, t2.LeaveTo
from tablename t1
  left join tablename t2 on t1.name = t1.name and t1.LeaveFrom < t2.LeaveFrom

(如果某个用户只有1行,请执行LEFT JOIN。)