I use Row_Number , to select only the "recent/higher" section.IF OBJECT_ID('tempDB..#ClassAvge') IS NOT NULL DROP TABLE #ClassAvgeCREATE TABLE #ClassAvge( Student INT ,Grade TINYINT ,Section TINYINT)INSERT INTO #ClassAvge (Student,Grade,Section)VALUES(1236, 78 , 1) ,(4526, 83, 1) ,(7785, 99, 1) ,(4213, 72, 2) ,(4213, 65, 1) ,(7852, 86, 1) ,(8963, 73, 1) ,(3247, 95, 2) ,(3247, 58, 1)SELECT AVG(CAST(Grade AS FLOAT)) AS AVG_GradeFROM ( SELECT Student,Grade,Section ,ROW_NUMBER() OVER(PARTITION BY Student ORDER BY Section DESC) AS RN FROM #ClassAvge )AWHERE A.RN = 1
sabinWeb MCP