Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query that generates this

StudentID |Building |Period |Grade
12345           2            1       11        
12345           2            5       11
43210           2            1       12

But I need a way to group the Periods by the StudentID:

StudentID |Building |Period | Grade
12345           2            1,5     11
43210           2            1        12

This is my query:

select r.student_id [StudentID],
r.building [Building],
ab.attedancePeriod [Period],
r.grade [Grade]
from register r
inner join att_bottom ab on ab.student_id = r.student_id
where ab.building = '2'
and ab.attendance_c = 'T'

Any suggestions are more than welcomed!




Update
The query below retrieves all of the StudentID's assigned to building 2 regardless if they have an attendance code of T or not for the specific date (today's date).
It's assigning a NULL value to the ones that don't when I just need the ones that do (which will be the ones with a period).


SQL
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


Result

StudentID |Building |Period |Grade
12345           2            1 ,        11        
43210           2            1 ,         12
98775           2           NULL   10
Posted
Updated 30-Jan-18 19:59pm
v2

SQL Wizardry Part Seven - PIVOT and arbitrary lists of data[^] shows how to do this, using FOR XML.\


SQL
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:

SQL
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.
 
Share this answer
 
v3
Comments
Member 10539656 23-Jan-14 15:02pm    
Thanks for your response. I have updated my question based on your response and I was wondering if you had any suggestions for retrieving only the rows with values for Period instead of the ones that don't (the ones assigned a NULL value).
Christian Graus 23-Jan-14 15:03pm    
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)
AND ab.attendancePeriod is not null
FOR XML PATH ('') ) AS PERIOD,
r.GRADE

FROM register r

But, why are you turning dates in to string to compare them ? Why not

and convert(date, ab.attendance_date) = convert(date, getdate())

?
Member 10539656 23-Jan-14 15:11pm    
I tried that earlier " and ab.attendancePeriod is not Null " but it didn't work. I was converting the date because the date format is 2014-01-23 00:00:00.000 but I tried your way and it worked as well.
Christian Graus 23-Jan-14 15:13pm    
My way is a lot more efficient.

Start with this:

SELECT ',' + convert(varchar(2), grade)
FROM register
WHERE register.student_id = r.student_id
group BY month(student_id)
order by month(grade)

replace r.studentid with a value you know has nulls in it. Then experiment. Really, is not null HAS to work.
Did you check this ?
 
Share this answer
 
Comments
Jochen Arndt 31-Jan-18 2:56am    
How could he checked in 2014 an SO thread from 2016 without having a time machine?

Please avoid answering old questions. It will usually get downvoted as you might have recognised (it wasn't me).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900