I'm building a sql string in a vbScript by concantinating a selectField and a whereField stored in a table. My goal is to identify the youngest students in a family starting with the third student up to max students. I have the following:SELECT TOP ((SELECT COUNT(stuId) AS studentsInFamily FROM tblStudents WHERE (yrRecId = 1) AND (famId = 5)) - 2) stuIdFROM tblStudents AS tblStudents_1 WHERE (yrRecId = 1) AND (famId = 5) ORDER BY stuDOB DESC
This returns the exact list that I need. However, my problem is that I'm iterating through the stuIds in a family in my code and checking to see if the currentStuId in my loop is one of the stuIds in this list. So I need the above query to actually be a subquery in a WHERE clause where I compare the currentStuId with one of the values returned by the query above. Confused? I'm amazed if you follow that.I don't know if this helps but I've added the above query to a WHERE clause and set it equal to a family that has only 3 students. It works perfectly, I can identify the youngest student if there are 3 students in the family. However the query breaks when I use it on a family with more than 3 students because the subquery returns more than one row.the following works with 3 students or less but not more than 3...I'm doing a vb Replace function to replace the #TOKENS# with current values.SELECT COUNT(stuId) AS intRecordCount FROM tblStudentsWHERE (yrRecId = #YEAR#) AND (famId = #FAM#) AND (stuId = #STUD#) AND ((SELECT TOP ((SELECT COUNT(stuId) AS studentsInFamily FROM tblStudents WHERE (yrRecId = #YEAR#) AND (famId = #FAM#)) - 2) stuId FROM tblStudents AS tblStudents_1 WHERE (yrRecId = #YEAR#) AND (famId = #FAM#) ORDER BY stuDOB DESC) = #STUD#)
In short is there a reverse IN() function where I can say currentStuId IsInList(subquery)?