table 列的聚合和升序合并

aggregation and ascending merge of table columns

我该怎么做? 列的唯一组合是 Number + Element,Cond1..n 是 SUMming() 和 Variable1..n 也需要相等。

这是我的想法

你知道我怎样才能做到这一点吗?在真实的数据库中它有几千行。

SQL Fiddle: http://sqlfiddle.com/#!6/1a7d0/1

谢谢

您应该阅读 String Aggregation in the World of SQL Server 以更好地了解 Group_concat 在 sql 服务器中的工作原理。试试这个,

SELECT
     t.Number
     ,[KEY]= STUFF((
          SELECT ',' + tt.[key]
          FROM SourceTb tt
          WHERE t.Number = tt.Number and t.Element = tt.Element and t.Variable1 = tt.Variable1
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
    ,t.Element
    ,t.Variable1
    ,SUM(CAST(t.cond1 as INT))
    ,SUM(CAST(t.cond2 as INT))
FROM SourceTb t
GROUP BY t.Number
    ,t.Element
    ,t.Variable1
ORDER BY t.Number desc

HERE is working fiddle