Search code examples
sqlspatialite

SQL/SpatiaLite: Combining two tables containing some identical rows and keeping some nonidentical ones


I need to combine two tables. Both of them have three column names that match and some other ones. The data doesn't match. I'm not trying to do a join on the values - the best I could describe this would be selective appending. I tried union but that doesn't work due to the different columns.. can this be even done like this? Or would I first have to create a new table and then insert from the other two?

Image for clarification:

enter image description here


Solution

  • Try to use union this way:

    select somevalue1,somevalue2,somevalue3,value1_t1,value2_t1,cast(null as int) as value2_t2,cast(null as int) as value3_t2
    from table1
    union all
    select somevalue1,somevalue2,somevalue3,null,null,value2_t2,value3_t2
    from table2
    

    In 1st query you need convert not maching column to target format. In 2ng you can use null insetad of not maching column.