I'm an SQL newbie, so there might be a standard recipe for the question I'm about to ask or it might already have been answered. I don't know the proper nomenclature for describing the type of operation I'm after, so I didn't know what to search for.
So, back to my problem: I have a table with transformations of an ID column that is used in other tables. It does include consecutive transformations, possibly several chained such transformations. All rows have a date stamp. Here's a toy sample:
|---------|-------|------------|
| from_id | to_id | date |
|---------|-------|------------|
| 1001 | 2001 | 2019-01-01 |
| 1002 | 2002 | 2019-01-01 |
| 1003 | 2003 | 2019-02-02 |
| 2001 | 3001 | 2019-03-03 |
| 2002 | 3002 | 2019-03-03 |
| 3001 | 4001 | 2019-04-04 |
|---------|-------|------------|
From this data I would like to create two tables:
- A table linking every from_id to its last to_id. For my toy example i want the following:
|---------|-------------|
| from_id | final_to_id |
|---------|-------------|
| 1001 | 4001 |
| 1002 | 3002 |
| 1003 | 2003 |
| 2001 | 4001 |
| 2002 | 3002 |
| 3001 | 4001 |
|---------|-------------|
- I also want a table with all linked ID:s, in both directions. For my toy example:
|------|------|
| id_1 | id_2 |
|------|------|
| 1001 | 2001 |
| 1001 | 3001 |
| 1001 | 4001 |
| 1002 | 2002 |
| 1002 | 3002 |
| 1003 | 2003 |
| 2001 | 1001 |
| 2001 | 3001 |
| 2001 | 4001 |
| 2002 | 1002 |
| 2002 | 3002 |
| 2003 | 1003 |
| 3001 | 1001 |
| 3001 | 2001 |
| 3001 | 4001 |
| 3002 | 1002 |
| 3002 | 2002 |
| 4001 | 1001 |
| 4001 | 2001 |
| 4001 | 3001 |
|------|------|
These two results could of course be combined into just one table, where the relevant rows for result 1 is just highlighted by a flag.
So, how do I do this in SQL? Any help deeply appreciated. Note that I don't know how many steps the longest chain of transformations contains, but I do know that it's increasing from day to day.