Given the following product and substitution tables, how can I get an output with all possibilities?
Product Table
> Category | Year | Product Code
> :--------|------|-------------
> Animals | 1998 | A0001
> Sports | 2001 | A0002
Substitution Table
> Product From | Product To
> :------------|------------
> A0001 | A0003
> A0002 | A0004
> A0003 | A0005
> A0004 | A0006
> A0006 | A0007
Product A0001 was replaced for A0003 and then A0003 replaced for A0005.
Product A0002 was replaced for A0004, A0004 for A0006 and then A0006 for A0007.
The desired output is:
> Category | Year | Product From | Product To
> :--------|------|--------------|-----------
> Animals | 1998 | A0001 | A0003
> Animals | 1998 | A0003 | A0005
> Sports | 2001 | A0002 | A0004
> Sports | 2001 | A0004 | A0006
> Sports | 2001 | A0006 | A0007
I think COALESCE function may help me but I can't build the query.