[英]SQL tasks assignment round robin
I am trying to figure out the round robin assignment of tasks to users. 我试图找出任务的循环分配给用户。
I have a table of tasks and users and I want to assign new tasks to the users in the group but no user can have more than 2 tasks. 我有一个任务和用户表,我想向该组中的用户分配新任务,但是任何用户都不能超过2个任务。
I want to assign the first task to the user who has the oldest date time stamp. 我想将第一个任务分配给日期时间戳最早的用户。
Current setup: 当前设置:
#newtasks
taskname:
task6
task7
userid task last_update_date
11 task1 2018-05-29 15:30:17.410
22 task2 2018-05-30 15:30:17.410
22 task3 2018-05-31 15:30:17.410
33 task4 2018-06-01 15:30:17.410
What I want to see, userid 22 should not get a task as they have 2 tasks: 我想看到的是,用户ID 22应该没有任务,因为它们有2个任务:
#assignedtasks:
userid task last_update_date
11 task1 2018-05-29 15:30:17.410
11 task6 2018-06-01 16:30:17.410
22 task2 2018-05-30 15:30:17.410
22 task3 2018-05-31 15:30:17.410
33 task4 2018-06-01 15:30:17.410
33 task7 2018-06-01 16:30:17.410
Code to create tables: 创建表的代码:
IF Object_id ('TEMPDB..#newtasks') IS NOT NULL
DROP TABLE #newtasks
CREATE TABLE #newtasks
(
taskname VARCHAR(8)
)
INSERT INTO #newtasks
VALUES ('task6')
INSERT INTO #newtasks
VALUES ('task7')
SELECT * from #newtasks
IF Object_id ('TEMPDB..#assignedtasks') IS NOT NULL
DROP TABLE #assignedtasks
CREATE TABLE #assignedtasks
(
userid INT,
task VARCHAR(8),
last_update_date DATETIME
)
INSERT INTO #assignedtasks
VALUES ('11','task1',getdate()-4)
INSERT INTO #assignedtasks
VALUES ('22','task2',getdate()-3)
INSERT INTO #assignedtasks
VALUES ('22','task3',getdate()-2)
INSERT INTO #assignedtasks
VALUES ('33','task4',getdate()-1)
SELECT * FROM #assignedtasks
Maybe you want to join on the row numbers order by the oldest last_update_date
for the users and taskname
for the tasks (as no other column is there to order by). 也许你想用最古老的加入行号顺序last_update_date
为用户和taskname
的任务(因为没有其他列在那里ORDER BY)。 To exclude users who already have two tasks use HAVING count(*) = 1
. 要排除已经完成两个任务的用户,请使用HAVING count(*) = 1
。
INSERT INTO assignedtasks
(userid,
task,
last_update_date)
SELECT at.userid,
nt.taskname,
getdate()
FROM (SELECT at.userid,
ROW_NUMBER() OVER (ORDER BY max(at.last_update_date)) #
FROM assignedtasks at
GROUP BY at.userid
HAVING count(*) = 1) at
INNER JOIN (SELECT nt.taskname,
ROW_NUMBER() OVER (ORDER BY nt.taskname) #
FROM newtasks nt) nt
ON nt.# = at.#;
Still thinking a users table is missing though. 仍然认为用户表丢失了。 What if a user has finished all their tasks? 如果用户完成了所有任务该怎么办?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.