How to map the table data while inserting into another table in sql

  • nagaraj

    SSC Enthusiast

    Points: 117

    table1

    id   value

    2    20

    3    30

    4    40

    5    50

    6    60

    7    null

    the above table has to be inserted into another table2 with following fashion

    i,e: it has to insert the value corresponding to 2 into col1 when id =2 ,col2 when id=3  and so on.  note: insert empty when null is the value

     

    table2

    col1      col2    col3    col4    col5     col6

    20          30     40      50         60      empty

     

  • Thom A

    SSC Guru

    Points: 98326

    What have you tried so far? Have you had a look at PIVOT, or (what many prefer), using a Cross Tab?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.

  • Phil Parkin

    SSC Guru

    Points: 243688

    What would you insert if there are multiple source rows with the same Id? For example

    id   value

    2    20

    2    30

    Would this require two inserts of 20 and 30, or a single insert of 50 (SUM), 25 (AVG), 20 (MIN) ... or some other aggregate?

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

  • nagaraj

    SSC Enthusiast

    Points: 117

    There multipl wont be any scenario like multiple source rows with the same Id in my case

  • This was removed by the editor as SPAM

Viewing 5 posts - 1 through 5 (of 5 total)

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