Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Expert,

Need to assign tasks to staff.

1. The next assignment should be assigned to the staff with the least.

How do you accomplish this in MS SQL SERVER 2008 ?


Master Table
-------------

idno name
--------------------------
0001 JAMES
0002 PAUL
0003 MAY
0004 PAT


TASKS TABLE
-------------

IDNO TASKS-COMPLAINT
------------------------------
0001 John Smith
0001 Sarah Adams
0001 Michael Jones

0002 Godfred Arthur
0002 David Lawson

0003 Jennifer Bruce




The next assignment should be go to 0004 -PAT


Please provide the select statement which will pick him from the task table



Thanks

What I have tried:

I have reviewed a number of codes but to no avail
Posted
Updated 23-Mar-17 8:59am
v2
Comments
CHill60 23-Mar-17 12:31pm    
"reviewed a number of codes"? Have you tried writing anything?
CHill60 23-Mar-17 12:39pm    
Actually no, the next task should be assigned to 004 - PAT as they have no tasks at all assigned to them therefore that staff member has the "least" number of tasks

1 solution

As per my comment, the first time you run this the task should be assigned to 004 - PAT as they currently do not have any tasks at all assigned to them.

This code will do what you want:
SQL
UPDATE [tasks] SET AssignedTo = 
	(select TOP 1 idno
	from [Master] M left outer join [tasks] T on M.idno=T.AssignedTo
	group by idno
	order by COUNT(t.taskID) )
WHERE AssignedTo IS NULL

Things to note:
- the use of TOP 1 to only get a single idno
- The ORDER BY so the one with the least assignments filters to the top
- I used a table like this
SQL
creeate table [tasks]
(
	taskID int identity(1,1),
	Task nvarchar(150),
	AssignedTo int
)

- Note the WHERE clause so I only update the unassigned records

The first time you run this code you may get a warning about NULL values
Quote:
Warning: Null value is eliminated by an aggregate or other SET operation.
This can be ignored and will not appear once all entries on the [Master] table have at least one Task assigned to them

To be clear I used the following test data
SQL
insert into [Master] values
('JAMES'),   -- Id = 1 (Autogenerated)
('PAUL'),    -- Id = 2
('MAY'),     -- Id = 3
('PAT')      -- Id = 4

insert into [tasks] values
('John Smith',1),       -- assigned to JAMES
('Sarah Adams',1),      -- assigned to JAMES
('Michael Jones',1),    -- assigned to JAMES
('Godfred Arthur',2),   -- assigned to PAUL
('David Lawson',2),     -- assigned to PAUL
('Jennifer Bruce',3),   -- assigned to MAY
('new item',null)       -- assigned to NO-ONE!


So the starting point is:
JAMES has 3 tasks assigned
PAUL has 2 tasks assigned
MAY has 1 task assigned
PAT has no tasks assigned to them.
There are 7 tasks on the table, all but one of them are assigned to someone.

Extrapolating the code above this section
SQL
--select TOP 1 idno
SELECT idno, name, count(t.taskID)
	from [Master] M left outer join [tasks] T on M.idno=T.AssignedTo
	group by idno, name
	order by COUNT(t.taskID)
gives the results
4       PAT	0
3	MAY	1
2	PAUL	2
1	JAMES	3
So the bit I actually used
SQL
select TOP 1 idno
	from [Master] M left outer join [tasks] T on M.idno=T.AssignedTo
	group by idno
	order by COUNT(t.taskID)
will return the value 4.
That value will be assigned to all rows on the table where the AssignedTo column is null.

Importantly, as in your case in the comments, if no rows have a null value in the AssignedTo column then nothing will be updated and the counts for each row on the Master table will remain unchanged

If I add a new row
SQL
insert into tasks values ('item 2',null)
and run the code again, the second time through both MAY and PAT only have 1 task each. It's likely that MAY be assigned the new task simply because that Id is lower than PAT, but this is not guaranteed.
 
Share this answer
 
v2
Comments
Member 12770648 24-Mar-17 1:13am    
I run it but it was not able to generate '0004' 'PAT'
Member 12770648 24-Mar-17 1:14am    
create table [Ktasks]
(
taskID int identity(1,1),
Task nvarchar(150),
AssignedTo int
)

INSERT INTO [Ktasks] VALUES('1','JOHN SMITH' ,1)
INSERT INTO [Ktasks] VALUES('1','SARAH ADAMS',1)
INSERT INTO [Ktasks] VALUES('1','MIC JONES' ,1)
INSERT INTO [Ktasks] VALUES('1','GODFRED ARTHUR' ,1)
INSERT INTO [Ktasks] VALUES('1','DAVID LAWSON' ,1)
INSERT INTO [Ktasks] VALUES('1','JENNY BRUCE' ,1)

create table [Master]
(
idno varchar(10),
name varchar(15),
)

Insert Into Master Values ('0001','JAMES')
Insert Into Master Values ('0002','PAUL')
Insert Into Master Values ('0003','MAY')
Insert Into Master Values ('0004','PAT')



UPDATE [Ktasks] SET AssignedTo =
(select TOP 1 idno
from [Master] M left outer join [Ktasks] T on M.idno=T.AssignedTo
group by idno
order by COUNT(t.taskID) )
WHERE AssignedTo IS NULL

select * from Ktasks
CHill60 24-Mar-17 4:01am    
Well for starters this code won't run as you've copied my table schema with an identity column and then tried to insert a number for that column (the same number for all rows).
The next reason that nothing will happen is you have no unassigned tasks on the task table so there is nothing to do.
My code (as posted) works perfectly - I tested it carefully before I posted it.
CHill60 24-Mar-17 5:14am    
I've updated my solution to try and explain to you what is going on and why my code does work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900