日期:2014-05-17 浏览次数:20469 次
DECLARE @t1 TABLE(CarID INT) DECLARE @t2 TABLE(id INT,perCarID INT,scort INT) INSERT INTO @t1 SELECT 1 UNION ALL SELECT 2 INSERT INTO @t2 SELECT '1','1','98' UNION ALL SELECT '2','2','50' UNION ALL SELECT '3','2','68' UNION ALL SELECT '4','1','70' UNION ALL SELECT '5','2','80' UNION ALL SELECT '6','1','92' ;WITH CTE AS ( SELECT * FROM @t1 t1 CROSS APPLY ( SELECT TOP 2 scort FROM @t2 WHERE t1.CarID=perCarID ORDER BY scort DESC ) t2 ) SELECT CarID,scort=STUFF((SELECT ','+CAST(scort AS VARCHAR) FROM CTE WHERE CarID=c.CarID FOR XML PATH('') ),1,1,'' ) FROM CTE c GROUP BY c.CarID