在SQL查询中动态插入额外的行

时间:2013-12-24 16:16:06

标签: sql sql-server-2012 interpolation

我正在使用SQL Server 2012.我有以下查询:

SELECT p.PortfolioGroupID, p.PortfolioGroupCode, p.DisplayOrder, p.MemberCode, m.ContactCode, m.Custom01, 'Separator' As 'PDFType'
  FROM [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
  LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioInterestedParty] m
  on p.memberid = m.PortfolioID
WHERE m.ContactCode is not null
and p.PortfolioGroupCode like '%_Package'
  order by m.ContactCode, p.MemberCode

当前结果集

PortfolioGroupID    PortfolioGroupCode  DisplayOrder    MemberCode  ContactCode Custom01    PDFType
11224   Test1_Package   677 test1   recipient1_ind  DVAAnnual   Separator
11224   Test1_Package   730 test2   recipient1_ind  DVAAnnual   Separator
11221   Test2_Package   1825    test3   recipient2_ind  DVAAnnual   Separator
11221   Test2_Package   1826    test4   recipient2_ind  DVAAnnual   Separator
11221   Test2_Package   1827    test5   recipient2_ind  DVAAnnual   Separator

所需的结果集

PortfolioGroupID    PortfolioGroupCode  DisplayOrder    MemberCode  ContactCode Custom01    PDFType
11224   Test1_Package   677 test1   recipient1_ind  DVAAnnual   Cover
11224   Test1_Package   677 test1   recipient1_ind  DVAAnnual   Separator
11224   Test1_Package   677 test1   recipient1_ind  DVAAnnual   Report
11224   Test1_Package   730 test2   recipient1_ind  DVAAnnual   Separator
11224   Test1_Package   730 test2   recipient1_ind  DVAAnnual   Report
11221   Test2_Package   1825    test3   recipient2_ind  DVAAnnual   Cover
11221   Test2_Package   1825    test3   recipient2_ind  DVAAnnual   Separator
11221   Test2_Package   1825    test3   recipient2_ind  DVAAnnual   Report
11221   Test2_Package   1826    test4   recipient2_ind  DVAAnnual   Separator
11221   Test2_Package   1826    test4   recipient2_ind  DVAAnnual   Report
11221   Test2_Package   1827    test5   recipient2_ind  DVAAnnual   Separator
11221   Test2_Package   1827    test5   recipient2_ind  DVAAnnual   Report

对于PortfolioGroupID列中新的PortfolioGroupID指示的组的每个实例。 (在此示例中有2个组11224和11221)我需要添加一行作为第一个行,其中包含单词封面,然后放置单词分隔符。所有其他列与之前放置的行相同。

对于组中的每一行,我需要插入/替换一行,并在我放置单词分隔符报告 >。所有其他列与其后面的行相同。

如果出于某种原因,在报告之前放置“分隔符”这个词更容易,我可以将“分隔符”的硬编码值更改为“报告”。

这可能吗?

1 个答案:

答案 0 :(得分:1)

第一步是添加RowNumber列以标识每个PorfoilioGroupID的第一行:

SELECT  p.PortfolioGroupID, 
        p.PortfolioGroupCode, 
        p.DisplayOrder, 
        p.MemberCode, 
        m.ContactCode, 
        m.Custom01, 
        RowNumber = ROW_NUMBER() OVER(PARTITION BY P.PortfolioGroupID ORDER BY m.ContactCode, p.MemberCode)
FROM    [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
        LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioInterestedParty] m
            ON p.memberid = m.PortfolioID
WHERE   m.ContactCode is not null
AND     p.PortfolioGroupCode like '%_Package'
ORDER BY m.ContactCode, p.MemberCode;

然后,您可以使用带有3个值(Cover,Sperator,Report)的表值构造函数将结果集加入,并添加一个where子句以确保仅显示第一行的“Cover”:

WITH Data AS
(   SELECT  p.PortfolioGroupID, 
            p.PortfolioGroupCode, 
            p.DisplayOrder, 
            p.MemberCode, 
            m.ContactCode, 
            m.Custom01, 
            RowNumber = ROW_NUMBER() OVER(PARTITION BY P.PortfolioGroupID ORDER BY p.DisplayOrder)
    FROM    [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
            LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioInterestedParty] m
                ON p.memberid = m.PortfolioID
    WHERE   m.ContactCode is not null
    AND     p.PortfolioGroupCode like '%_Package'
)
SELECT  d.PortfolioGroupID, 
        d.PortfolioGroupCode, 
        d.DisplayOrder, 
        d.MemberCode, 
        d.ContactCode, 
        d.Custom01, 
        t.PDFType
FROM    Data d
        CROSS JOIN 
        (   VALUES
                ('Cover'),
                ('Seperator'),
                ('Report')
        ) t (PDFType)
WHERE   d.RowNumber = 1
OR      t.PDFType != 'Cover'
ORDER BY ContactCode, MemberCode;

<强> Example on SQL Fiddle