benmort81 Posted September 1, 2015 Posted September 1, 2015 Hello All, I have a table "Staff" and a related table "Lesson_Observations". A Staff record has many Lesson_Observation records. Each lesson observation is attributed a score. I have a working ExecuteSQL calculation that finds the average of all lesson observations scores for staff in a specific Faculty ("English" in the example below) and only where the Staff record has the attribute "Active". Let ( [ $query = "SELECT AVG(L.obCritAverageVal) FROM Lesson_Observation L INNER JOIN Staff S ON L.FK_StaffCode = S.PK_StaffCode WHERE S.Fk_FacName = ? AND S.Status = ?" ; $result = ExecuteSQL ( $query ; "" ; "" ; "English"; "Active") ]; $result ) Each Lesson_Observation record has an attribute "Observation_Date". Is there a way I can modify the query above to find the average of only the MOST RECENT lesson_observation for each member of staff in a Faculty where that member is staff is still Active? Thank you! Ben
eos Posted September 1, 2015 Posted September 1, 2015 (edited) Try ExecuteSQL ( " SELECT AVG ( O.score ) FROM Observations O JOIN Staff S On S.id = O.id_staff WHERE S.faculty = ? AND S.status = ? AND O.TS = ( SELECT MAX (O2.TS) FROM Observations O2 WHERE O2.id_staff = O.id_staff ) " ; "" ; "" ; "English" ; "Active" ) which you obviously need to adapt to your nomenclature Edited September 1, 2015 by eos 1
benmort81 Posted September 1, 2015 Author Posted September 1, 2015 Genius! This is neat! I had worked around it by sorting the relationship in descending date order and then adding a calculated field Last_Ob_Score to the Staff table which = observation score from the Observation table - then doing a simple ExecuteSQL from the Last_Ob_Score field. No need for the extra database overhead with your query. Thanks
Recommended Posts
This topic is 3644 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now