I am trying to show all the job positions held by the staff member except 'Relief Teacher' - if the staff is holding more than one job positions. And show Relief teacher if the teacher (staff is holding only one job title of Relief Teacher).
I used a case expression and then used Select statement in it which uses group by, in order to count the number of job positions held by the staff member. But I get an error message saying the subquery returned more than 1 value.
Also, I am trying to exclude 'Relief Teacher' from the JobPositions When I am displaying all the Job Positions when using STUFF() function. But I am not able to get that.
The second When in Case Statement will show all the job positions when using STUFF. It does not exclude Relief Teacher title from there.
SELECT jobpositions.staffid,
jobpositions.StaffNameExternal,
(Case when ((select Count(StaffId) from dbo.vStaffJobPositions group by staffid) =1 and (jobpositiondescription like 'Relief Teacher'))
then 'Relief Teacher'
when ((select Count(StaffId) from dbo.vStaffJobPositions group by staffid) >1 )
then (STUFF((SELECT ' / ' + JobPositionDescription
FROM dbo.vStaffJobPositions
FOR XML PATH('')),1,3, ''))
end) as StaffPosition
FROM dbo.vStaffJobPositions JobPositions
I have 2 problems with the above code-
- The Count and groupBy are giving error of Subquery returning more than one value.
- I want to show only the Job positions held by the staff except Relief Teacher using STUFF()- which is stuff different positions held by them. But I cant EXCLUDE 'Relief Teacher' from there (when they hold more than one job title)