Little SQL Server Tricks: The STRING_AGG() Function
When it comes to reporting, we often get some special requirements to transform data into a specific form. For one report we had a Workforce table with data that looks like this:
| EmployeeId | FirstName | LastName | DepartmentId |
|---|---|---|---|
| 1001 | John | Smith | 10 |
| 1002 | Jane | Doe | 10 |
| 1003 | Max | Miller | 20 |
For the report, we need to group the employees by DepartementId, and combine all employees with their EmployeeId into this form:
| DepartmentId | EmployeeFormatted |
|---|---|
| 10 | John Smith (1001), Jane Doe (1002) |
| 20 | Max Miller (1003) |
