OK I'm going to give you half the answer - because I'm going home in five minutes.What you're trying to do is to concatenate table results into a single string.In order to try to test this I created a flat table of your results - you will obviously have to tailor this to suit your own database set-up. I did this here:SELECT 'Dave' as Name, 100 as Prod_ID, 50 as GroupID INTO #customerUNION ALLSELECT 'Dave',98,50 UNION ALLSELECT 'Dave',86,100 UNION ALLSELECT 'Dave',101,100
Now what you need to do is to concatenate all the Prod_IDs for each Group_ID. This code below can be converted into a function to do this:DECLARE @GroupID INTSET @GroupID = 100 -- this is just to test the queryDECLARE @strOutput VARCHAR(100) -- this will be your outputSET @strOutput = '' -- this is set to '' because you'll be appending values to itSELECT @strOutput = @strOutput + PID + ', ' -- this appends each value onto the end of the variable FROM (SELECT CONVERT(VARCHAR,Prod_ID) as PID FROM #customer WHERE GroupID= @GroupID) xSELECT @strOutput
When you pass in the Group_ID of 100, it will return '86,101'.Well it doesn't actually, it returns '86,101,' so you'll have to remove the trailing comma. You'll also need to account for when nothing is returned as that might cause it to fail.Sorry I can't be more helpful but hopefully this will provide the basis for you to work the rest out for yourself.. . or someone else to jump in and finish it off.Good luck.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club