mardi 17 juillet 2018

SQL conditional statements replaced with management table

I have the following scenario that is bothering me for a while. Lets say I have this table:

Mockup Table

I have another table that has the same and much more columns and empty result column. On the base of the mockup table i have to go and check if Column 1 and 2 and 3 match up. Then I need to take the result from the mockup table and record it in the first table. The problem is that the mockup table will change so the logic behind this should be dynamic. Furthermore, there might be a missing column in the mockup table.

For example if Column 1 and Column 3 are presented but Column 2 is missing, I should match 1 and 3 and take the result, regarding of what value I have in column 2 in the first table. Also, there might me more than 1 result, for example If Column 3 = something Result is 1, but if Column 2 = something else then Result is 2. So I have to combine those results in something like [1,2].

I have created script with a lot of case -> when -> then -> else but they are all hardcoded and when the mockup table change i will have to go and change the script which is no go.

My idea is to get each record from the first table and match the column in the second table then take the result from the mockup table for each match and record it in the first table. Any idea will be welcome..

Aucun commentaire:

Enregistrer un commentaire