I'm having difficulty updating records in a database based on the most recent date, and I'm looking for some recommendations. By the way, I'm new to SQL.
As a background, I have a Windows Forms application with SQL Express and I use ADO.NET to interact with the database. The application is designed so that the user can track the attendance of employees at various courses that should be attended on a periodic basis (for example, every 6 months, every year, etc.). For example, they can select data to see the last time employees attended a given course, and also update the dates of attendance if the employee recently completed the course.
I have three data tables:
- EmployeeDetailsTable - a simple list of employee names, email address, etc., each with a unique identifier
- CourseDetailsTable - A simple list of courses, each with a unique identifier (for example, 1, 2, 3, etc.).
- AttendanceRecordsTable - has 3 columns {EmployeeID, CourseID, AttendanceDate, Comments}
For any given course, the employee will have a history of attendance, that is, if the course should be attended every year, then they will have one record for as many years as they were in the company.
What I want to do is update the Comments field for this employee and this course based on the last visit date. What is the “correct” SQL syntax for this?
I tried a lot of things (like below), but can't make it work:
UPDATE AttendanceRecordsTable
SET Comments = @Comments
WHERE AttendanceRecordsTable.EmployeeID = (SELECT EmployeeDetailsTable.EmployeeID FROM EmployeeDetailsTable WHERE (EmployeeDetailsTable.LastName =@ParameterLastName AND EmployeeDetailsTable.FirstName =@ParameterFirstName)
AND AttendanceRecordsTable.CourseID = (SELECT CourseDetailsTable.CourseID FROM CourseDetailsTable WHERE CourseDetailsTable.CourseName =@CourseName))
GROUP BY MAX(AttendanceRecordsTable.LastDate)
, MAX , GROUP BY. HAVING, .
- ? "" ?