This basically does it. It might need some tweaking to get it to exactly what you need:Declare @output TABLE(agentid int, agentmail varchar(30), ordernos varchar(100), OrderDate varchar(100))declare @orderID int, @agentMail varchar(20), @orderNo varchar(20), @orderDate datetime, @agentID int, @orderNos varchar(100), @orderDates varchar(100)SET @agentID = 0WHILE EXISTS (SELECT NULL FROM zOrders WHERE agentID > @agentID)BEGIN SELECT TOP 1 @agentID = agentID FROM zOrders WHERE agentID > @agentID ORDER BY agentID ASC SELECT @orderID = 0, @orderNos = '', @orderDates = '' WHILE EXISTS (SELECT NULL FROM zOrders WHERE agentID = @agentID AND orderid > @orderID) BEGIN SELECT TOP 1 @orderID = orderID, @agentMail = agentMail, @orderNo = orderNo, @orderDate = orderDate FROM zOrders WHERE orderID > @orderID AND agentID = @agentID ORDER BY orderID ASC SELECT @orderNos = @orderNos + @orderNo + ', ', @orderDates = @orderDates + Convert(varchar, @orderDate, 101) + ', ' END SELECT @orderNos = LEFT(@orderNos, LEN(@orderNos)-1), @orderDates = LEFT(@orderDates, LEN(@orderDates)-1) INSERT INTO @output(agentid, agentmail, ordernos, OrderDate) VALUES (@agentID, @agentMail, @orderNos, @orderDates)ENDSELECT * FROM @output
HTH,Tim