If you provide test tables in the form of CREATE TABLE statements and sample data in the form of INSERT INTO statements I would be happy to provide some working code to help you solve the issue.
Without that I can refer you to this article:
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns By Jeff Moden][/url]
edit: fix url
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato