简体   繁体   中英

Compare two tables and get output with unmatched data with respective column name

I have two tables similar to following construct. I need to compare both and get the output as shown in results.

Here I am comparing column1 with column4, column2 with column5 and column3 and column6.

Table1:

Key column1 column2 column3
1    4       5       6
2    2       5       8
3    4       5       10
4    4       6       10

Table2:

Key column4 column5 column6
1    6       5       6
2    2       5       8
3    4       5       10
4    4       8       10

Result should be:

Key1 Table2ColumnName Table1ColumnValue Table2ColumnValue
1    column4              4                  6
4    column5              6                  8    

Note : In actual case, I have tables with huge number of columns to compare. Column names in both table are different.

Could someone kindly guide on how to achieve the above requirement.

I don't think this will be solved with a SQL query since you want to do this dynamically and there isn't a consistent naming for the columns. The example you gave is short and sweet but you mentioned you have 'tables with huge number of columns to compare'.

You might be better off importing each table you want to compare into an Excel spreadsheet (one table per sheet) and doing some VLOOKUP formulas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM