Rows to Columns

  • Guys, Stuck on the following

    Source TableA:

    tel            name         charge1     charge2    charge3   address

    ------------------------------------------------------------------

    12345        John            10             2         15       Birmingham

    12356        Peter           15             0         17       London

    98765        Mike            12              0          0         Lincoln

    Destination TableB

    tel            name         charge1    address

    ----------------------------------------------

    12345        John            10       Birmingham

    12345        John            2        Birmingham

    12345        John            15       Birmingham

    12356        Peter           15       London

    12356        Peter           17       London

    98765        Mike            12       Lincoln

    As from above, I am trying to loop through table A and insert charge 1,2 and 3 into the

    same column into table b, so there will be a record for each charge, but should only

    enter a record if the charge is > 0, so above  Mike only has one record, Peter has 2

    and John has 3

    Any help would be greatly appreciated

     

     

  • Insert into TableB (Tel, name, charge1, adress)

    Select tel, name, charge1, adress from dbo.YourTable where Charge1 > 0

    UNION ALL

    Select tel, name, charge2, adress from dbo.YourTable where Charge2 > 0

    UNION ALL

    Select tel, name, charge3, adress from dbo.YourTable where Charge3 > 0

  • Thanks Remi

  • NP.. R U normalizing your tables or just doing the select?

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply