I have a view in which there is a list of tasks in it, with data such as the one to whom they are assigned, and at the stage in which they are. I need to write a stored procedure that returns the number of tasks, each of which each person has at each stage.
So far I have this (simplified):
DECLARE @ResultTable table
(
StaffName nvarchar(100),
Stage1Count int,
Stage2Count int
)
INSERT INTO @ResultTable (StaffName, Stage1Count)
SELECT StaffName, COUNT(*) FROM ViewJob
WHERE InStage1 = 1
GROUP BY StaffName
INSERT INTO @ResultTable (StaffName, Stage2Count)
SELECT StaffName, COUNT(*) FROM ViewJob
WHERE InStage2 = 1
GROUP BY StaffName
The problem is that the lines are not concatenated. Therefore, if an employee has tasks in stages 1 and stage2, there are two lines in @ResultTable. I would really like to update the row if it exists for the employee, and insert a new row if it does not exist.
Does anyone know how to do this, or can suggest a different approach? I would really like to avoid using cursors to repeat in the list of users (but this is my return option).
SQL Server 2005.
: @Lee: , InStage1 = 1 . WHERE DateStarted NOT NULL DateFinished IS NULL.
: @BCS: , . UPDATE.