Skip to content
Advertisement

Need to get the value from a column whose column name is based on a value in another table

Table A has columns ID, COL1, COL2, COL3. Table B has columns AID, ColumnName.

I need to get the [ColumnName] value in Table A based on the value of [ColumnName] in Table B. In the example below:

For ID 1, I need to get the value of column COL1 (This is the value of [ColumnName] for AID 1 in Table B).

For ID 2, I need to get the value of column COL3 (This is the value of [ColumnName] for AID 2 in Table B).

Table A

Table B

Desired Result:

How can I do that ? Thank you.

Advertisement

Answer

Unpivot then join

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement