I came up with one problem where I am looking for a solution as I have two tables:
Table1: Users
UserID | TotalTasks |
---|---|
A3 | 12 |
A4 | 14 |
A5 | 11 |
Table2: Tasks
TaskID | NewTask |
---|---|
1 | 4 |
2 | 5 |
3 | 3 |
4 | 2 |
Output:
TaskID | NewTask | UserID | UpdatedTasks |
---|---|---|---|
1 | 4 | A5 | 15 |
2 | 5 | A3 | 17 |
3 | 3 | A4 | 17 |
4 | 2 | A5 | 17 |
Here for table Tasks, first TaskID in order, I need to check which user has minimum TotalTasks from the users table and add that userID and NewTask + TotalTasks under the column "UpdatedTasks" and for the next TaskID, again need to check userID with updated minimum totalTasks and repeat the same process for each row without using loop as this I need to run in synapse sql.
I tried with recursive cte but not getting right output
2
Answers
As per this ask,
I have tried your scenario with Recursive CTE, and you see that it is not supported.
In the Synapse SQL, you can use loops as a workaround like below to achieve your requirement. Here, it needs a temporary table which you can drop after getting the desired result.
Output:
But as the data size increases, this approach might take more time compared to the recursive approach. So, you can try reading the tables as spark dataframe views in synapse notebook and in the spark SQL, you can use recursive CTE to get the desired data and write it back to the Synapse SQL tables.
Don’t know synapse, but if it supports basic analytic functions [ Row_Number Over() and Max() Over() ] then you could try something like here:
fiddle