SQL语句改写: 根据A列的ID相同的合并成一条数据,并且根据C列的数字从小到大排序 万分感谢!!! 50
1个回答
展开全部
以下语句 SQL Server 2008 测试通过:
;WITH tmpTable AS (
SELECT 1 AS A, '张三' AS B, 24 AS C, 'DT' AS D
UNION ALL
SELECT 1 AS A, '张三' AS B, 25 AS C, 'AL' AS D
UNION ALL
SELECT 1 AS A, '张三' AS B, 26 AS C, 'SL' AS D
UNION ALL
SELECT 2 AS A, '李四' AS B, 25 AS C, 'SL' AS D
UNION ALL
SELECT 2 AS A, '李四' AS B, 24 AS C, 'DT' AS D
UNION ALL
SELECT 2 AS A, '李四' AS B, 26 AS C, 'AL' AS D
), tmpTable_hz AS (
SELECT DISTINCT A, B FROM tmpTable
)
SELECT a.A, a.B
, STUFF((SELECT ',' +CAST(aa.C AS VARCHAR) FROM tmpTable aa WHERE aa.A =a.A ORDER BY C FOR XML PATH('')), 1, 1, '') AS C
, STUFF((SELECT ',' +CAST(aa.D AS VARCHAR) FROM tmpTable aa WHERE aa.A =a.A ORDER BY C FOR XML PATH('')), 1, 1, '') AS D
FROM tmpTable_hz a
GROUP BY a.A, a.B
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询