DECLARE @IdList varchar(8000) SELECT @IdList = ISNULL(@IdList + ', ', '') + StudentId FROM survey WHERE StudentId IN ( SELECT StudentId FROM survey WHERE interest IN (3,45) GROUP BY StudentId HAVING COUNT(*) = 2 ) GROUP BY StudentId SELECT @IdListAs you can see, ther are two "variables" in this query: the list of interests '(3,45)' and number of valuses in the list - '2'.
>660 0014388 20.0 >689 0169109 2.0 >691 0169108 2.0 >696 0018490 2.0 >699 0022895 5.0 >700 0022895 6.0 >701 0168595 2.0 >702 0168595 5.0 >703 0168595 6.0 >>