-1

So we have a roster table like this:

 Course, Class, Period, Location, Teacher ID, Student ID
ECC31|Counting and Cardinality (K3)|2(B)|No data|100|1248|15179
ECC31|Counting and Cardinality (K3)|2(B)|No data|100|1248|15244
ECC31|Counting and Cardinality (K3)|2(B)|No data|100|1248|15257
.....

Same Class/Different Students. Is there an SQL approach to have it concatenated with commas? Say one row for each class? Was thinking of Group by Function with a concatenator?

Example of output

ECC31|Counting and Cardinality (K3)|2(B)|No data|100|1248|15179,15244,15257
ECC32|Counting and Cardinality 2 (K3)|3(B)|No data|100|1267|15123,15143,15432

Thanks!

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Enrique Bravo
  • 133
  • 1
  • 1
  • 11

1 Answers1

1

In MySQL you can use the GROUP_CONCAT() function.

SELECT Course, Class, Period, Location, TeacherID, GROUP_CONCAT(StudentID)
FROM roster
GROUP BY Course, Class, Period, Location, TeacherID

Note that the string of concatenated student ids has a default length limit of 1024 characters. You can increase this using the group_concat_max_len option.

As you can read in the manual, the elements of the list are separated by commas by default, but you can customize this. Refer to the manual for details and examples.

Microsoft SQL Server does not have the GROUP_CONCAT() function. This is why commenters were asking what database you're using. Different implementations of SQL databases have different features and functions.

See past questions like Simulating group_concat MySQL function in Microsoft SQL Server 2005? for solutions in Microsoft SQL Server.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828