Let's say I have data like so:
PersonID Goal
-------- ----
1 one
1 two
1 three
2 alpha
2 beta
PersonID Note
-------- ----
1 hello
1 world
2 i
2 like
2 squirrels
And I want to put all the goals and notes for each person into a single row:
PersonID Goal 1 Goal 2 Goal 3 Note 1 Note 2 Note 3
-------- ------ ------ ------ ------ ------ ------
1 one two three hello world
2 alpha beta i like squirrels
With the maximum goal and note numbers computed to be equal to the maximum count of goals and notes belonging to any one person - so if I add a third person who has five goals then two more goal columns will be added, for instance.
Is there any way to write a SQL query that combines the rows in this fashion? Or do I have to do that sort of data manipulation in the application itself (as I am currently), which is slower?
edit: I'm using SQL Server and it would be OK to instead have a delimited list, e.g.
PersonID Goals Notes
-------- ------ ------
1 one|two|three hello|world
2 alpha|beta i|like|squirrels