Consolidating Data in SQL Server 2005

I need to consolidate data from our SQL 2005 database to make it easier to create a catalog.  Currently the data is stored in a table named Matrix_Items as show in the picture Matrix_Items.  I need to be able to get the data into a columns labeled Matrix_Group_Code and Matrix_Item_Code in a table named GRS_Catalog.  I need to have the data in the below format.

Matrix_Group_Code               Matrix_Item_Code
1147                                       11470-0001, 11471-0001, 1172-0001, 11473-0001, 1147cle
1148                                       11480-0001, 11481-0001, 1182-0001, 11483-0001, 11484-0001


I have been doing research online and I have been able to create a code that gets the data in the correct format when I execute it in a SQL Query but I don't know how to get it to insert into the table, I have attached the code that I have created.  Any help would be greatly appreciated.

SELECT p1.Matrix_Group_Code,

       ( SELECT Item_Code + ',' 

           FROM Everest_Everest.dbo.MATRIX_ITEMS p2

          WHERE p2.Matrix_Group_Code = p1.Matrix_Group_Code

          ORDER BY Matrix_Group_Code

            FOR XML PATH('') ) AS Matrix_Item_Code 

      FROM Everest_Everest.dbo.MATRIX_ITEMS p1

      GROUP BY Matrix_Group_Code;

Open in new window

Matrix-Items.JPG
chrisphilippsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ephraim WangoyaCommented:

Try
insert into GRS_Catalog 
SELECT p1.Matrix_Group_Code,

       ( SELECT Item_Code + ',' 

           FROM Everest_Everest.dbo.MATRIX_ITEMS p2

          WHERE p2.Matrix_Group_Code = p1.Matrix_Group_Code

          ORDER BY Matrix_Group_Code

            FOR XML PATH('') ) AS Matrix_Item_Code 

      FROM Everest_Everest.dbo.MATRIX_ITEMS p1

      GROUP BY Matrix_Group_Code;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.