I have a table like this:
Position | EmpNumber | Name | Status
1 | 1234 | Fred | Active
2 | 1235 | Popeye | Active
2 | 1236 | Olive | Active
2 | 1237 | Scooby | Active
2 | 1238 | Homer | Active
3 | 1239 | Bart | Active
3 | 1240 | Tom | Active
4 | 1241 | Jerry | Active
5 | 1242 | Bugs Bunny | Active
I want to query that, and pivot it, so I only get the position (column 1) once and columns for each EmpNumber (column 2) aliased as Emp1, Emp2 etc. the worse case will ever be 4 numbers against each position. So my required results would be:
Position | Emp1 | Emp2 | Emp3 | Emp4
1 | Fred | NULL | NULL | NULL
2 | Popeye | Olive | Scooby | Homer
3 | Bart | Tom | NULL | NULL
4 | Jerry | NULL | NULL | NULL
5 | Bugs Bunny | NULL | NULL | NULL
I want all of the attributes for each row, maybe as single columns or maybe concatenated into a single field i.e. "1234,Fred,Active", but have just used the Name column above to keep it simple for now.
Also interested in how to make it dynamic just in case additional rows ever featured against the position.