SELECT B.TaskID ,
LEFT(SamList, LEN(SamList) - 1) AS ResultList
FROM ( SELECT TaskID ,
( SELECT SampleNumber + ','
FROM TrialProcedure_Detail
WHERE TaskID = A.TaskID
group by TaskID,SampleNumber
FOR
XML PATH('')
) AS SamList
FROM TrialProcedure_Detail A
GROUP BY TaskID
) B