declare @users table (user_id int, user_name varchar(50))
insert into @users values
(1234, 'completed_two'),
(1235, 'completed_cero'),
(1236, 'completed_one'),
(1237, 'completed_two_no_survey')
declare @class_registration table
(reg_id int, class_id int, class_type varchar(50), user_id int, attendance bit)
insert into @class_registration values
(1, 1, 'stress', 1234, 1),
(2, 1, 'stress', 1236, 1),
(3, 2, 'who_cares', 1234, 1),
(4, 1, 'stress', 1235, 0),
(5, 5, 'nutrition', 1236, 1),
(6, 9, 'who_cares_2', 1237, 1),
(7, 10, 'return_of_the_care', 1237, 1)
declare @surveys table
(survey_id int, user_id int, class_id int, survey_type varchar(50))
insert into @surveys values
(1, 1234, 1, 'Pre Workshop Survey'),
(2, 1236, 1, 'Pre Workshop Survey'),
(3, 1235, 1, 'Pre Workshop Survey'),
(4, 1236, 2, 'Pre Workshop Survey'),
(5, 1234, 1, 'Post Workshop Survey'),
(6, 1236, 2, 'Post Workshop Survey')
select u.user_id, u.user_name
from @users u
inner join
(
select c.user_id
from @class_registration as c
inner join @surveys as s
on c.class_id = s.class_id and
c.user_id = s.user_id
where
class_type in ('nutrition', 'stress') and
survey_type = 'Post Workshop Survey' and
attendance = 1
union all
select c.user_id
from @class_registration as c
where
class_type not in ('nutrition', 'stress') and
attendance = 1
) as cc
on u.user_id = cc.user_id
group by u.user_id, u.user_name
having count(*) > 1