I am trying to use the template table that has 2 columns to create a desired Result table using:
- The first column: a list of all the column names in Table A.
- The Second Column : a list of all column names in Table B.
From the table below I want to use the column names from TableA and Table B and compare the values joining them on their ID.
Template Table
| TableA | TableB |
| ------------ | ------------ |
| Id | Id |
| Fruits1 | Fruits2 |
| Vegetables1 | Vegetables2 |
Table A
Id | Fruits1 | Vegetables1 |
---|---|---|
1 | Apple | Carrot |
2 | Banana | Pumpkin |
Table B
Id | Fruits2 | Vegetable2 |
---|---|---|
1 | Apple | Carrot |
2 | Banana | Onion |
Desired Result
ID | Fruits1 | Fruits2 | Comparison1 | Vegetable1 | Vegetable2 | Comparison2 |
---|---|---|---|---|---|---|
1 | Apple | Apple | True | Carrot | Carrot | True |
2 | Banana | Banana | True | Pumpkin | Onion | False |
How can I use the Template table to join Table A and Table B using the ID to produce the above table?
I tried Unpivoting the mapping table but I could not find a way.
1条答案
按热度按时间vhmi4jdf1#
It seems the best way to do this would be dynamic SQL.
You need to build up a list of columns