To see how the query that JLS wrote works, please see the following article.
http://www.sqlservercentral.com/Forums/Topic935472-392-1.aspx
To learn how to do the same in a dynamic condition where the column names aren't always known at design time, please see the following article.
http://www.sqlservercentral.com/articles/Crosstab/65048/
--Jeff Moden
Change is inevitable... Change for the better is not.