使用sql合并Datarows

时间:2016-04-05 04:47:49

标签: sql vb.net

我有这样一张桌子: Datatable

我需要像这样修改它: - Required Table

我必须合并具有相同操作类型,机器和附件的操作名称,然后总结合并操作的[sam]。

我尝试了这个,但收到错误

SELECT [operation_type],
       [operation_name],
       [machine_type],
       [attachment],
       Stuff((SELECT '+' + t2.operation_name 
          FROM   ob 
          WHERE  [operation_type] = t2.[operation_type] 
                 AND [machine_type] = t2.[machine_type] 
                 AND [attachment] = t2.[attachment] 
          FOR xml path ('')), 1, 1, '') AS NAME 
FROM   ob AS t2 
GROUP  BY operation_type

1 个答案:

答案 0 :(得分:0)

SELECT 
    operation_type,
    STUFF((SELECT '+' + ob.operation_name 
           FROM ob
           WHERE  
               operation_type = t2.operation_type
               AND machine_type = t2.machine_type 
               AND attachment = t2.attachment 
           FOR XML PATH ('')), 1, 1, '') AS 'Operation_Name' ,
    machine_type,
    attachment,
    SUM(SAM) AS 'SAM'
FROM   
    ob AS t2 
GROUP BY 
    t2.operation_type, t2.machine_type, t2.attachment

谢谢!