多行合并成一排,并追加取决于行数列(Combining multiple rows into one

2019-08-17 12:42发布

我想同一个表的多个行合并为一个。

我有这样一个示例表

Col1    Col2    Col3    Col4    Col5    Col6
1   BH1 CB  12  CC  CC - Conveyor Mal.
1   BH1 CB  104 ZC  ZC - Full/Emp Signal Mal.
1   BH1 CB  109 HD  HD - Dosing Pipe Blocked
2   BH2 CD  14  HP  HP- Laptop
2   BH2 CD  21  DE  DE -Dell
3   BH1 BC  41  FE  FE- Fuji
4   BH9 BC  95  SN  SN-Sony

我的预计了放应该是这样的一些事情

Col1    Col2    Col3    Col4    Col5    Col6    Col7    Col8    Col9    Col10   Col11   Col12
1   BH1 CB  12  CC  CC - Conveyor Mal.  104 ZC  ZC - Full/Emp Signal Mal.   109 HD  HD - Dosing Pipe Blocked
2   BH2 CD  14  HP  HP- Laptop  21  DE  DE -Dell             
3   BH1 BC  41  FE  FE- Fuji                         
4   BH9 BC  95  SN  SN-Sony  

提前致谢。

Answer 1:

您可以使用选项与dynamicSQL, 适用()和PIVOT运算符。 在这种情况下列顺序结果,按列进行排序COL4。

DECLARE @cols nvarchar(max),
        @query nvarchar(max)
SELECT @cols = 
  STUFF((SELECT x.ColName
         FROM (
               SELECT ',' + QUOTENAME('Col' + CAST(3 + ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3 ORDER BY Col4) AS nvarchar(10))) AS ColName,
                      ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3 ORDER BY Col4) AS rn
               FROM dbo.test31 CROSS APPLY (VALUES('Col'), ('Col'), ('Col')) o(Col)) x
         GROUP BY x.ColName, x.rn
         ORDER BY x.rn                   
         FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
SET @query = 
  'SELECT * FROM 
    (      
     SELECT t.Col1, t.Col2, t.Col3,            
            ''Col'' + CAST(3 + ROW_NUMBER() OVER(PARTITION BY t.Col1, t.Col2, t.Col3 ORDER BY t.Col4) AS nvarchar(10)) AS ColName,            
            COALESCE(CAST(o.oCol4 AS nvarchar(10)), o.oCol5, o.oCol6) AS ListValues            
     FROM dbo.test31 t CROSS APPLY (
                                    SELECT oCol4, oCol5, oCol6
                                    FROM (VALUES (t.Col4, NULL, NULL),
                                                 (NULL, t.Col5, NULL),
                                                 (NULL, NULL, t.Col6))
                                    x(oCol4, oCol5, oCol6)
                                    ) o
     ) x
     PIVOT
      (
       MAX(ListValues) FOR ColName IN(' + @cols + ')
       ) p'
EXEC (@query)

演示上SQLFiddle

选项的列名这样的“工厂,品牌,区域1,DownTime1,Reasons1,区域2,DownTime2,Reasons2等等。”

DECLARE @cols nvarchar(max),
        @query nvarchar(max)
SELECT @cols = 
  STUFF((SELECT ',' + QUOTENAME(o.Col) AS ColName                
         FROM
          (
           SELECT *, CAST(ROW_NUMBER() OVER(PARTITION BY Plant, Brand ORDER BY Area) AS nvarchar(10)) AS rn
           FROM dbo.test35) t CROSS APPLY (VALUES('Area' + t.rn), ('DownTime' + t.rn), ('Reasons' + t.rn)
           ) o(Col)
         GROUP BY o.Col, t.rn
         ORDER BY t.rn                   
         FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')       

SET @query = 
  'SELECT * FROM 
    (      
     SELECT t.Plant, t.Brand, o.ColName,            
            COALESCE(CAST(o.Area AS nvarchar(10)), o.DownTime, o.Reasons) AS ListValues
     FROM
      (
       SELECT Plant, Brand, Area, DownTime, Reasons,
              CAST(ROW_NUMBER() OVER(PARTITION BY Plant, Brand ORDER BY Area) AS nvarchar(10)) AS rn            
       FROM dbo.test35) t CROSS APPLY (
                                      SELECT Area, DownTime, Reasons, ColName
                                      FROM (VALUES (t.Area, NULL, NULL, ''Area'' + t.rn),
                                                   (NULL, t.DownTime, NULL, ''DownTime'' + t.rn),
                                                   (NULL, NULL, t.Reasons, ''Reasons'' + t.rn))
                                      x(Area, DownTime, Reasons, ColName)
                                      ) o
      ) x
     PIVOT
      (
       MAX(ListValues) FOR ColName IN(' + @cols + ')
       ) p'
EXEC (@query) 

演示上SQLFiddle



文章来源: Combining multiple rows into one row and appending the columns depending on the number of rows