SQL Wizardry Part Seven - PIVOT and arbitrary lists of data[
^] shows how to do this, using FOR XML.\
select r.student_id [StudentID],
r.building [Building],
ab.attedancePeriod [Period],
STUFF
(
(
SELECT ',' + convert(varchar(2), grade)
FROM register
WHERE register.student_id = r.student_id
group BY month(student_id)
order by month(grade)
FOR XML PATH('')
), 1, 1, ''
) AS Grades
from register r
inner join att_bottom ab on ab.student_id = r.student_id
where ab.building = '2'
and ab.attendance_c = 'T'
group by r.student_id
is my guess, but without the source tables, it's hard to know.
Oh, you may want to try this:
select r.student_id [StudentID],
r.building [Building],
(select ab.attendancePeriod + ','
from att_bottom ab
where ab.student_id = r.student_id
and ab.building = '2'
and ab.attendance_c ='T'
AND CONVERT(varchar,ab.attendance_date,102) = convert(varchar,getdate(),102)
FOR XML PATH ('') ) AS PERIOD,
r.GRADE
FROM register r
inner join att_bottom a on a.student_id = r.student_id
where a.attendance = 'T'
I think the issue is, you're filtering on the inner level, but not the outer level. The filter on the inner level may be able to be removed.