将两个透视查询结果合并到一个表

时间:2017-03-20 15:57:28

标签: sql

我编写了两个生成以下T1和T2的数据透视查询,我试图将结果合并为一个单独的表。我被困在这里,因为以下查询在数据透视查询中使用两个不同的值列进行搜索。我如何将它们作为一个单独的表加入?

select
                 FileID, 
                 Worksheet,
                 [Index], 
                 ISNULL([Attachment],0) as [Attachment]

        from (select  FileID, 
                  Worksheet,
                  [Index],
                  Label, 
                  Data
        from IT.[dbo].[IT_DATA]
        where FileID = 18265
        and Worksheet = 'Summary' 
        and [Index] <>'0') t       

        PIVOT
        (max([Data]) 
              for [Label] in ([Attachment])
        )piv 

查询2:

select
                 FileID, 
                 Worksheet,
                 Label, 
                 LOB,
                 ISNULL([1],0) as 1_Share,
                 ISNULL([2],0) as 2_Share,  
                 ISNULL([3],0) as 3_Share,
                 ISNULL([4],0) as 4_Share,
                 ISNULL([5],0) as 5_Share       

        from (select   FileID, 
                  Worksheet,
                  [Index],
                  Label, 
                  LOB, 
                  Data
        from IT.[dbo].[IT_DATA]
        where FileID = 18265       
        and Label = 'Share') s
        PIVOT
        (max([Data]) 
              for [Index] in ([1],[2],[3],[4],[5])
        )piv 

表1: 下表总是只有这五个索引,我需要将它们与表2的15个LOB列合并。我正在尝试合并两个表的结果并使最终表看起来像图片文件

FileID      Worksheet   Index   Attachment

  18265     Summary       1           0

  18265     Summary       2           0 

  18265     Summary       3          200000000

  18265     Summary       4           0

  18265     Summary       5           0

enter image description here

0 个答案:

没有答案