I have a table
| id | repeat customer id | store | date |
|---|---|---|---|
| 1 | A | 07-19-22 | |
| 2 | A | 07-19-22 | |
| 3 | A | 07-19-22 |
| id | repeat customer id | store | date |
|---|---|---|---|
| 1 | B | 07-19-22 | |
| 2 | B | 07-19-22 | |
| 3 | 1 | B | 07-19-22 |
| 4 | B | 07-19-22 |
and more tables from other store
The problem here is
- all stores start with id 1
- repeat customer have new id in id column and their original id is retained in repeat customer id column
I have to concatenated all the tables and also keep track of repeating customer for analytics. I have joined all tables using UNION ALL and also created a dummy id column using SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS NEW_ID, * FROM CTE, but I have no clue how to capture and assign value to repeat customer id such that I get the table as below
| NEW_ID | id | new_repeat_customer_id | repeat customer id | store | date |
|---|---|---|---|---|---|
| 1 | 1 | A | 07-19-22 | ||
| 2 | 2 | A | 07-19-22 | ||
| 3 | 3 | A | 07-19-22 | ||
| 4 | 1 | B | 07-19-22 | ||
| 5 | 2 | B | 07-19-22 | ||
| 6 | 3 | 4 | 1 | B | 07-19-22 |
| 7 | 4 | B | 07-19-22 |