I have the most frustrating error that keeps coming up about an item not in the Group By Clause.
Column 'Task' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I have read multiple examples, but they don't appear to address my issue. Any assistance with this would be greatly appreciated.
I have a table of Tasks - listing the JobId, the Task and the Date the task was completed, as follows:
JobId Task TaskCompleted
1 A 01/01/2022
1 B 02/01/2022
1 C 03/01/2022
1 D
2 A 02/01/2022
2 B 04/01/2022
2 C
3 A 05/01/2022
3 B 07/01/2022
I am after a list of the most recent completed task for each JobId. So the result I am after is as follows:
Job LastTask CompletedDate
1 C 03/01/2022
2 B 04/01/2022
3 B 07/01/2022
I would have expected the following would work:
select JobId as Job, Task as LastTask, max(TaskCompleted) as CompletedDate
from TaskTable
where
TaskCompleted is not null
group by JobId --, Task
However I get the Group By Clause error. If I add the "Task" column to the Group by I end up getting multiple entries per job (1 entry for each task)
I've read up on this but can't seem to solve it.
If you can assist with this it would be greatly appreciated.