我有两张桌子(Temp1& finalTemp)

时间:2015-10-19 14:21:50

标签: sql

我有两张桌子(Temp1& finalTemp)

的temp1

AccId  Name  address city GuarantorId
1       abc   xyz     a       11
1       qwe   asd     a       115
2        kk    aa     t        21
3       t       u      p       96

现在我希望我的finalTemp像

AccId  Name1  address1 city1  Name2  address2 city2 Name3  address3 city3
 1     abc     xyz      a     qwe      asd      a     null   null    null
 2     kk       aa      t      null    null     null  null    null   null
3       t        u       p

1 个答案:

答案 0 :(得分:1)

自我LEFT JOIN

select t1.*,t2.*
from temp t1
  left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name

或者,扩展,双自我左连接:

select t1.*,t2.*,t3.*
from temp t1
  left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name
  left join temp t3 on t2.AccId = t3.AccId and t2.name < t3.name