I have the following table, TableA, with some data
ColA ColB
0 5
0 6
I have another table, TableB, with some data
ColC ColD ColE
5 10 5
5 15 10
6 20 10
ColC is the foreign key for ColB. For each row in TableA I need to return ColA, ColB and ColD. The row to select in TableB depends on the value of ColE that is set through a parameter.
For example: If my parameter is set to 5, I should get two rows back as follows:
ColA ColB ColD
0 5 10
0 6 null
However, if my parameter is anything other than 5 and no row exists in TableB, it uses a parameter value of 5 instead and retrieves the value from ColB (if a row exists).
I tried several things but can’t seem to come up with the solution. Thank you for your help!
Try this: