Consider these tables:
Department Table
DepartmentID | DepartmentName |
1 | Sales |
2 | IT |
3 | Finance |
Employee Table
EmployeeID | DepartmentID | EmployeeName |
1 | 1 | Sales Person 1 |
2 | 1 | Sales Person 2 |
3 | 2 | Developer 1 |
4 | 2 | Developer 2 |
5 | 2 | DBA 1 |
Now you want the results like this:
DepartmentName | EmployeeList |
Sales | Sales Person 1, Sales Person 2 |
IT | Developer 1, Developer 2, DBA |
Finance | NULL |
There are couple of ways of doing this:
- Loop through for each department and construct the string and store in a temporary table; Finally retrieve from that temporary table
- Use the same mechanism but use all departments at a time. Need to loop through the rest of the employees
- Create a CLR function to do this.
No comments:
Post a Comment