I have a table that has user a user_id and a new record for each return reason for that user. As show here:
| user_id | return_reason | |--------- |-------------- | | 1 | broken | | 2 | changed mind | | 2 | overpriced | | 3 | changed mind | | 4 | changed mind |
What I would like to do is generate a foreign key for each combination of values that are applicable in a new table and apply that key to the user_id in a new table. Effectively creating a many to many relationship. The result would look like so:
Dimension Table ->
| reason_id | return_reason | |----------- |--------------- | | 1 | broken | | 2 | changed mind | | 2 | overpriced | | 3 | changed mind |
Fact Table ->
| user_id | reason_id | |--------- |----------- | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 3 |
My thought process is to iterate through the table with a cursor, but this seems like a standard problem and therefore has a more efficient way of doing this. Is there a specific name for this type of problem? I also thought about pivoting and unpivoting. But that didn't seem too clean either. Any help or reference to articles in how to process this is appreciated.