Identify the users with minimum votes and allocate new votes to that user in Synapse SQL

Avanish Tomar 20 Reputation points
2024-12-27T17:28:31.5566667+00:00

Hello Experts 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 thte smae process for each row without using loop as this I need to run in synapse sql. Any help much appriciated. thanks!

I tried with recursive cte but not getting right output

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,098 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,243 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,671 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 114.8K Reputation points MVP
    2024-12-31T09:46:36.3333333+00:00

    Here is a solution that gives the expected results from your sample data. However, I am bending the rules! What I do is that I order the users on the current total number of tasks, and then I assign then tasks from the Tasks table in a round-robin fashion.

    Depending on the business rules for your actual problem, this may be good enough - or it may be entirely inappropriate. But at least it is a relatively simple solution, and it is a single statement.

    You never said whether you are using a serverless or dedicated pool, but this runs in the master database of my Synapse workspace, which I believe is the serverless pool.

    CREATE TABLE #Users (
        UserID VARCHAR(2),
        TotalTasks INT
    );
    -- Insert data into Users table
    INSERT INTO #Users (UserID, TotalTasks) VALUES
    ('A3', 12),
    ('A4', 14),
    ('A5', 11);
    -- Create the Tasks table
    CREATE TABLE #Tasks (
        TaskID INT,
        NewTask INT
    );
    -- Insert data into Tasks table
    INSERT INTO #Tasks (TaskID, NewTask) VALUES
    (1, 4),
    (2, 5),
    (3, 3),
    (4, 2);
    
    WITH UserNumbering AS (
       SELECT UserID, TotalTasks, 
              QueueOrder = row_number() OVER(ORDER BY TotalTasks),
              NoOfUsers = COUNT(*) OVER()
       FROM   #Users
    ), TaskNumbering AS (
       SELECT TaskID, NewTask,
              TaskNo = row_number() OVER (ORDER BY TaskID) - 1
       FROM   #Tasks
    )
    SELECT T.TaskID, T.NewTask, U.UserID,
           UpdatedTasks = U.TotalTasks + SUM(T.NewTask) OVER(PARTITION BY U.UserID
                                                             ORDER BY T.TaskNo 
                                                             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    FROM   TaskNumbering T
    JOIN   UserNumbering U ON U.QueueOrder = T.TaskNo % U.NoOfUsers + 1
    ORDER  BY T.TaskID
    go
    DROP TABLE #Tasks
    DROP TABLE #Users
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.