SELECT DISTINCT Dense_Rank() OVER (partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) As [SortOrder], Dense_Rank() OVER (partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) % 3 As [Remainder], CASE WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) = 1 THEN 'Include' WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) % 3 = 0 THEN 'Include' WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) = 4 THEN 'First Alternate' WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) = 7 THEN 'Second Alternate' WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) = 10 THEN 'Third Alternate' WHEN Dense_Rank() OVER(partition by ssa.SectionIdentifier ORDER BY s.StudentUniqueID) IN (SELECT COUNT(ssa2.StudentUSI) FROM edfi.studentsectionassociation ssa2 WHERE ssa2.SectionIdentifier = ssa.SectionIdentifier) THEN 'Include' END AS [Sample], eo.NameOfInstitution, st.LastSurname as TeacherLastName, st.FirstName as TeacherFirstName, c.CourseCode, c.CourseTitle, ssa.sectionidentifier, scp.ClassPeriodName, dt1.CodeValue, s.StudentUniqueID, s.LastSurname as StudentLastName, s.FirstName as StudentFirstName from edfi.StudentSectionAssociation ssa left join edfi.EducationOrganization eo on eo.EducationOrganizationId=ssa.SchoolId left join edfi.Student s on s.StudentUSI=ssa.StudentUSI left join edfi.Session sess on sess.SessionName=ssa.SessionName and sess.Schoolyear=ssa.SchoolYear and sess.SchoolId=ssa.SchoolId left join edfi.Descriptor dt1 on sess.TermDescriptorId=dt1.DescriptorId left join edfi.StaffSectionAssociation stsa on stsa.SessionName=ssa.SessionName and ssa.LocalCourseCode=stsa.LocalCourseCode and ssa.SectionIdentifier=stsa.SectionIdentifier and ssa.SchoolYear=stsa.SchoolYear and ssa.SchoolId=stsa.SchoolId left join edfi.Staff st on st.StaffUSI=stsa.StaffUSI left join edfi.CourseOffering co on co.LocalCourseCode=ssa.LocalCourseCode left join edfi.Course c on c.CourseCode=co.CourseCode left join edfi.SectionClassPeriod scp on scp.LocalCourseCode=ssa.LocalCourseCode and scp.SchoolId=ssa.SchoolId and scp.SchoolYear=ssa.SchoolYear and scp.SectionIdentifier=ssa.SectionIdentifier and scp.SessionName=ssa.SessionName left join edfi.StudentSchoolAssociation sscha on ssa.Schoolid=sscha.Schoolid and ssa.StudentUSI=sscha.StudentUSI left join edfi.Descriptor dt2 on sscha.EntryGradeLevelDescriptorId=dt2.DescriptorId and dt2.codevalue='Kindergarten' order by ssa.SectionIdentifier,s.StudentUniqueID