0

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

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 列,仅用于区分需要复制数据的组。

非常感谢您的帮助。

4

2 回答 2

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以确保请求的订单将需要更多的努力。

于 2019-01-09T11:47:51.347 回答
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
于 2019-01-17T11:50:10.513 回答