locked
SQL query issues: how to join the same columns from two different tables V1 and V2 RRS feed

  • Question

  • User-1188570427 posted

    I have a view that is set up off a routing table.  I need to make a new routing table for V2 because the routing steps have changed. There is one new column in the V2 table that the V1 table did not have.  How can I keep the view the same where it can pull from both V1 and V2 tables so I can display my legacy routing data and then also include the new routing V2 data?

    Thursday, February 4, 2016 2:33 PM

Answers

  • User77042963 posted
     Select col1,col2, colNew from V2
     Union ALL
      Select col1,col2, null as colNew from V1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 4, 2016 3:13 PM

All replies

  • User77042963 posted
     Select col1,col2, colNew from V2
     Union ALL
      Select col1,col2, null as colNew from V1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 4, 2016 3:13 PM
  • User-1188570427 posted

     Select col1,col2, colNew from V2
     Union ALL
      Select col1,col2, null as colNew from V1

    This didn't work :-(

    There are 2 new records in my V2 routing table and the view is bring back 3 records. I assume that 3rd record is coming from the V1 routing table that has no data for that main record id.

    Make sense?

    Thursday, February 4, 2016 3:27 PM
  • User77042963 posted

    Can you show your sample table V1 and V2  with sample data? What is the query that you have now?

    Thursday, February 4, 2016 3:35 PM
  • User-1188570427 posted

    Can you show your sample table V1 and V2  with sample data? What is the query that you have now?

    Let me make 2 tables. I can't send the actual query/info because it is work.  Give me a few.

    Thursday, February 4, 2016 3:46 PM
  • User-219423983 posted

    Hi tvb2727,

    Let me make 2 tables. I can't send the actual query/info because it is work.  Give me a few.

    Have you solved your issue now? If not, you’d better provide two sample tables with records and give the expected output result, so that we could know what you want clearly.

    There are 2 new records in my V2 routing table and the view is bring back 3 records. I assume that 3rd record is coming from the V1 routing table that has no data for that main record id.

    Do you mean you get an unexpected record by using the SQL query string provided by limno above? This query just combines the two result into one and gives you a great idea to implement your needs. I suggest you could first check whether the third column is null or not. If it’s null, this record would come from the table “v1”, or it comes from “v2”. If you don’t want to get this unexpected one, you could add a where condition to exclude it.

    Best Regards,

    Weibo Zhang

    Friday, February 19, 2016 1:51 AM