I'm trying to run the SUM of MAX values, and then updating them into a column. I think the derived table is the right way to go, but now I keep getting a multi-part id could not be bound error that I dont know how to get round.
I'm running this on SSMS, and it will be my Db for a PowerApp. I'm essentially trying to take the distinct or MAX values of Assessment Hours from multiple Units, then add those together, grouped by the Staff ID number. I'm working with a derived table to try and mix the two aggregate functions.
WITH pretotalAssessment as
(
SELECT dbo.StaffTotals.Entry_ID, ISNULL(SUM(maxAssess),0) AS maxAssessHours
FROM
(
SELECT dbo.StaffTotals.Entry_ID, dbo.Units.[Unit Name],
ISNULL(MAX(dbo.Units.[Assessment Hours]),0) AS maxAssess
FROM dbo.Units
INNER JOIN dbo.StaffTotals ON dbo.StaffTotals.Entry_ID = dbo.Units.Entry_ID
GROUP BY dbo.StaffTotals.Entry_ID,dbo.Units.[Unit Name]
)Units
)
UPDATE preStaffTotals
SET preStaffTotals.Assessment = pretotalAssessment.maxAssessHours
FROM dbo.StaffTotals AS preStaffTotals
INNER JOIN pretotalAssessment ON preStaffTotals.Entry_ID = pretotalAssessment.Entry_ID;
My error is:
"The multi-part identifier "dbo.StaffTotals.Entry_ID" could not be bound."
I'm still quite new to SQL, so this is all a learning curve for me!