拆分记录并将数据复制到拆分记录中(奇数表结构)

时间:2019-01-09 11:34:34

标签: sql sql-server

我有一个看起来像这样的表(不要问。我没有创建它,但必须使用它)。我们称之为OddTable ...

Heading     Details           Issue1   Issue2   Issue3   Comments   BatchId
ItemID      SN1001                                                  10
Date Done   2018-12-18                                              10
Section1                        1         1        1                10
AreaA                           1         1                         10                                                                                                                                                                                                                                       
AreaB                                                               10
ItemID      SN1002                                                  11
ItemID      SN1003                                                  11
ItemID      SN1004                                                  11
Date Done   2018-12-11                                              11
Section1                                                  Test      11
AreaA                                     1               Stuff     11
AreaB                           1         1               Even More 11
ItemID      SN1005                                                  12
ItemID      SN1006                                                  12
Date Done   2018-12-11                                              12
Section1                                           1                12
AreaA                                     1        1      Blah      12
AreaB                           1                         Yada      12

试图选择如下所示的结果(将每个ItemID分成从批次中重复出来的自己的记录集):

Heading     Details           Issue1   Issue2   Issue3   Comments   
ItemID      SN1001                                                  
Date Done   2018-12-18                                              
Section1                        1         1        1                
AreaA                           1         1                                                                                                                                                                                                                                                                  
AreaB                                                               
ItemID      SN1002                                                  
Date Done   2018-12-11                                              
Section1                                                  Test      
AreaA                                     1               Stuff     
AreaB                           1         1               Even More 
ItemID      SN1003                                                  
Date Done   2018-12-11                                              
Section1                                                  Test      
AreaA                                     1               Stuff     
AreaB                           1         1               Even More 
ItemID      SN1004                                                  
Date Done   2018-12-11                                              
Section1                                                  Test      
AreaA                                     1               Stuff     
AreaB                           1         1               Even More 
ItemID      SN1005                                                  
Date Done   2018-12-11                                              
Section1                                           1                
AreaA                                     1        1      Blah      
AreaB                           1                         Yada
ItemID      SN1006                                                  
Date Done   2018-12-11                                              
Section1                                           1                
AreaA                                     1        1      Blah      
AreaB                           1                         Yada      

注意:结果中不需要BatchId列,它仅用于区分需要复制数据的组。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试使用cross apply

select t2.*
from table t1
cross apply (
  select 'ItemID' Heading, t1.Details, null Issue1, null Issue2, null Issue3, null Comments
  union all
  select *
  from table t3
  where t1.BatchId = t3.BathId and Heading in ('Date Done', 'Section1', 'AreaA', 'AreaB')
) t2
where t1.heading = 'ItemID'

但是,问题是SELECT的结果不是隐式排序的。因此,您可能会获得以某种方式交换的结果行。分配一些ID以确保请求的订单将需要更多的努力。

答案 1 :(得分:0)

花了一些时间考虑一下,并提出了这个解决方案(与@Sean Lange所建议的类似)。我想到的是将同一张表分成两部分,然后将它们内部连接在一起,这样我就得到了重复的数据。我敢肯定有人会指出一种更有效的方法来做到这一点,但是直到那时,这才是我所拥有的。查询如下:

SELECT 
    Part1.ItemId
    ,Part1.DateDone
    ,Part2.Area
    ,Issue1
    ,Issue2
    ,Issue3
    ,Comments
    ,Part2.BatchId
FROM
(SELECT 
    *
FROM( 
    SELECT
        CASE WHEN Heading = 'ItemID' THEN Details END as ItemId
        ,MAX(CASE WHEN Heading = 'Date Done' THEN Details END) OVER (PARTITION BY BatchId) as DateDone
        ,BatchId
    FROM 
        dbo.OddTable
) as StartingData
WHERE ItemId not like 'NULL'
) as Part1
inner join
(SELECT
    Heading as Area
    ,Issue1
    ,Issue2
    ,Issue3
    ,Comment
    ,BatchId
FROM
    dbo.OddTable
WHERE Heading in ('Section1','AreaA','AreaB')
) as Part2
ON Part1.BatchId = Part2.BatchId

ORDER BY Part1.ItemId,Part1.BatchId