Fetching results on the basis of column combination

Certified Associate Developer

Hello All,

I have a requirement to fetch values from two different views created in mssql to get the final result. The problem here is I don't have unique column in both the table, Hence I have to use column combinations to get the desired value. Here is the scenario

Suppose Table1 has 4 columns, Namely

col1   col2  col3   col4

Table2 also has 4 columns,Namely

col1   col2   col3   col4

 

Now I have to fetch the result on the basis of columns combination, Lets say If value in col1 and col2 of table1 is exactly same as the value of col1 and col2 in table two only then I have to retrieve the result. Can someone please let me know if there is a way to do this? I can not create view to merge this, So that option is not available for me.

  Discussion posts and replies are publicly visible

Parents
  • HI Vivek,

    You can create view, instead of comparing both columns individually from table 1 to table 2 , concatenate column 1 & 2 and make it one column for the both table 1 & 2. Now you will have one common column in each table . I think you can easily now compare both table and get the result. Here is the code for your reference :

    select tb1.*, tb2.* from Table 1 as tb1 join Table 2 as tb2 on concat(tb1.column1,tb1.column2)=concat(tb2.column1,tb2.column2).


    Regards
    Abhay Giri
Reply
  • HI Vivek,

    You can create view, instead of comparing both columns individually from table 1 to table 2 , concatenate column 1 & 2 and make it one column for the both table 1 & 2. Now you will have one common column in each table . I think you can easily now compare both table and get the result. Here is the code for your reference :

    select tb1.*, tb2.* from Table 1 as tb1 join Table 2 as tb2 on concat(tb1.column1,tb1.column2)=concat(tb2.column1,tb2.column2).


    Regards
    Abhay Giri
Children
No Data