sql - How to get values from rows on one table to column of other -


i have 2 tables looks :

first table

   customerid    customername      110             xyz                      111             abc     112             pqr      113             lmn                 

second table

    customerid         phonenumber        110             9823983298329        111             9996709760760       110             0495054905495         112             8394893489843       113             0932023090900         111             0930290909999       113             8993293288888       112             9828239882388 

what want table structure similar :

  customerid      customername      phonenum1             phonenum2      110             xyz             9823983298329        0495054905495       111             abc             9996709760760        0930290909999     112             pqr             8394893489843        9828239882388     113             lmn             0932023090900        8993293288888 

i stuck logic here , if join 2 tables using inner join output table have multiple customerid's not required here , highly appreciable.
thanx in advance.

this form of pivot; need column pivot on. can result using conditional aggregation , row_number():

select t1.customerid, t1.customername,        max(case when seqnum = 1 phonenmber end) phonenmber1,        max(case when seqnum = 2 phonenmber end) phonenmber2 table1 t1 left join      (select t2.*,              row_number() on (partition customerid order customerid) seqnum       table2 t2      ) t2      on t1.customerid = t2.customerid group t1.customerid, t1.customername; 

Comments