I had a few examples of recursive CTEs from around the web to build from and so I could easily get a CTE to recusively look through a table of lookups and replace a destination string with the corresponding string. The added complication was that there was actually several lookup tables combined into one lookup table and first I had to link the look up table to an id in the destination table. An example of the two tables are shown below:
| ItemID | Lookup | Replacement |
| 1 | MX | MN |
| 1 | MY | CY |
| 1 | SB | DG |
| 2 | HG | TY |
| 2 | HD | OK |
| ItemID | String | Result |
| 1 | MX+MY+SB | |
| 2 | HD/HG |
The desired result is to have MN+CY+DG in row 1 column 3 and OK/TY in row 2 column 3. The first idea was to just let the lookup navigate all the way through table 2 but this may have replaced strings in the result that were not linked to the item (i.e. If row2 of table 2 contained SB) and would be really inefficent as in the real system these tables could potentially contain 1000s of rows.
No comments:
Post a Comment