我有两个表,一个用于员工,另一个用于当前和过去的活动。我需要一个函数/查询,根据类型为下一个活动选择一个员工。这是样本数据:
表员工
EmployeesID UserName Type Available
1 Bill Clerical 1
2 Ann Clerical 1
3 John Technical 1
4 Jack Technical 0
5 Mary Clerical 0
6 Sam Technical 0
7 Mark Clerical 1
8 Andy Clerical 1
9 Rick Clerical 1
10 Nancy Clerical 1
活动
ActivityID EmployeesID Status Task_Datetime
101 2 Complete 6/11/2016 10:00
102 1 Complete 6/11/2016 10:12
103 3 Complete 6/11/2016 10:24
104 4 Complete 6/11/2016 10:36
105 7 Complete 6/11/2016 10:48
106 6 Complete 6/11/2016 11:00
107 5 Complete 6/11/2016 11:12
108 8 Complete 6/11/2016 11:24
109 10 Complete 6/11/2016 11:36
110 9 Complete 6/11/2016 11:48
111 5 Complete 6/11/2016 12:00
112 8 Complete 6/11/2016 12:12
113 3 Complete 6/11/2016 12:24
114 4 Complete 6/11/2016 12:36
115 1 Complete 6/11/2016 12:48
116 6 Complete 6/11/2016 13:00
117 7 Complete 6/11/2016 13:12
118 2 Complete 6/11/2016 13:24
119 9 Complete 6/11/2016 13:36
120 10 In Progress 6/11/2016 13:48
121 1 In Progress 6/11/2016 14:00
122 2 Complete 6/11/2016 14:12
123 3 Complete 6/11/2016 14:24
124 4 Complete 6/11/2016 14:36
125 6 In Progress 6/11/2016 14:48
需要在没有正在进行的活动的情况下分配给可用的员工匹配类型 如果一切正在进行中,则返回0 如果有多个可用,则分配给已分配的那个。
因此,如果下一次分配是文书,请不要在进行中取1或10 但是最后一位完成了2,5,7,8,9(其他未被占用的人)的文职人员是8人(因为没有,所以跳过5人)。
如果下一次分配是技术性的,那么应该转到John,如果john不可用,则应该返回null。
到目前为止写了这样的东西:
function: NextEmployee('Clerical')
Select EmployeeID from Employees e , Activities a where e.available= 1 and e.type='Clerical' and
答案 0 :(得分:2)
此代码使用window function查找每位员工的最后一项活动,然后检查此活动是否为“完成”状态:
DECLARE @Type VARCHAR( 20 )
SET @Type = 'Clerical'
SELECT TOP 1 TaskType, EmployeesID, Task_DateTime
FROM(
SELECT E.EmployeesID, E.TaskType, Task_DateTime, ActivityStatus, ROW_NUMBER() OVER( PARTITION BY A.EmployeesID ORDER BY Task_DateTime DESC ) AS LatestEmploeeTask
FROM #Activities AS A
INNER JOIN #Employees AS E ON A.EmployeesID = E.EmployeesID
WHERE Available = 1 ) AS a
WHERE LatestEmploeeTask = 1 AND ActivityStatus = 'Complete'
AND TaskType = @Type
ORDER BY Task_DateTime ASC
我必须将样本数据转换为可用的表数据。代码如下:
CREATE TABLE #Employees( EmployeesID INT, UserName VARCHAR( 10 ), TaskType VARCHAR( 20 ), Available BIT )
CREATE TABLE #Activities( ActivityID INT, EmployeesID INT, ActivityStatus VARCHAR( 20 ), Task_DATETIME DATETIME )
INSERT INTO #Employees
VALUES
( 1, 'Bill', 'Clerical', 1 ),
( 2, 'Ann', 'Clerical', 1 ),
( 3, 'John', 'Technical', 1 ),
( 4, 'Jack', 'Technical', 0 ),
( 5, 'Mary', 'Clerical', 0 ),
( 6, 'Sam', 'Technical', 0 ),
( 7, 'Mark', 'Clerical', 1 ),
( 8, 'Andy', 'Clerical', 1 ),
( 9, 'Rick', 'Clerical', 1 ),
( 10, 'Nancy', 'Clerical', 1 )
INSERT INTO #Activities
VALUES
( 101, 2, 'Complete', '6/11/2016 10:00'),
( 102, 1, 'Complete', '6/11/2016 10:12'),
( 103, 3, 'Complete', '6/11/2016 10:24'),
( 104, 4, 'Complete', '6/11/2016 10:36'),
( 105, 7, 'Complete', '6/11/2016 10:48'),
( 106, 6, 'Complete', '6/11/2016 11:00'),
( 107, 5, 'Complete', '6/11/2016 11:12'),
( 108, 8, 'Complete', '6/11/2016 11:24'),
( 109, 10, 'Complete', '6/11/2016 11:36'),
( 110, 9, 'Complete', '6/11/2016 11:48'),
( 111, 5, 'Complete', '6/11/2016 12:00'),
( 112, 8, 'Complete', '6/11/2016 12:12'),
( 113, 3, 'Complete', '6/11/2016 12:24'),
( 114, 4, 'Complete', '6/11/2016 12:36'),
( 115, 1, 'Complete', '6/11/2016 12:48'),
( 116, 6, 'Complete', '6/11/2016 13:00'),
( 117, 7, 'Complete', '6/11/2016 13:12'),
( 118, 2, 'Complete', '6/11/2016 13:24'),
( 119, 9, 'Complete', '6/11/2016 13:36'),
( 120, 10, 'In Progress', '6/11/2016 13:48'),
( 121, 1, 'In Progress', '6/11/2016 14:00'),
( 122, 2, 'Complete', '6/11/2016 14:12'),
( 123, 3, 'Complete', '6/11/2016 14:24'),
( 124, 4, 'Complete', '6/11/2016 14:36'),
( 125, 6, 'In Progress', '6/11/2016 14:48')