透视结果集

时间:2019-10-15 18:29:19

标签: sql sql-server

我试图获取一个结果集,该结果集包含具有GUID,百分比值,名称和位标志的数据行,并对数据进行旋转以使其由GUID分组,并具有一组列。其余字段。每个GUID在原始结果集中最多可以包含五行数据,但可以不包含任何数据,也可以不包含一个数字,介于一到五个之间。

所需的输出在列方面应如下所示:

GUID,Name1,Perc1,Flag1,Name2,Perc2,Flag2,Name3,Perc3,Flag3,Name4,Perc4,Flag4,Name5,Perc5,Flag5

我已经能够对数据进行PIVOT,以便可以使名称出现在GUID上,但是我不确定如何扩展该名称以将每个名称的其他字段(Perc和Flag)放入结果集。

SELECT Id, Insurer1, Insurer2, Insurer3, Insurer4, Isurer5
FROM (
   SELECT Policy.Id,
      BinderInsurer.[Percentage] As BinderInsurerPerc,
      BinderInsurer.LeadInsurer,
      Account.RegisteredCompanyName As BinderInsurerName,
      ROW_NUMBER() OVER (PARTITION BY Policy.Id ORDER BY BinderInsurer.[Percentage] DESC) AS PolCount,
      'Insurer' + CONVERT(VARCHAR(6), ROW_NUMBER() OVER (PARTITION BY Policy.Id ORDER BY BinderInsurer.[Percentage] DESC)) AS PivotKey
   FROM [Policy]
        Left Join Scheme On Scheme.Id = [Policy].SchemeId
    Left Join BinderAgreement On BinderAgreement.Id = Scheme.BinderAgreementId
    Left Join BinderSection On BinderSection.Id = Scheme.BinderSectionId
    Left Join BinderPeriod On BinderPeriod.Id = Scheme.BinderPeriodId
    Left Join BinderInsurer On BinderAgreement.Id = BinderInsurer.BinderAgreementId
    Left Join Account On Account.Id = BinderInsurer.AccountId
    ) a
PIVOT
(
MAX(a.BinderInsurerName)
FOR PivotKey IN ([Insurer1], [Insurer2], [Insurer3], [Insurer4], 
[Isurer5])
)     AS pvt

我希望输出是一个表格,每个GUID都有一行,并根据输入情况在适当的地方填充其余的列。

1 个答案:

答案 0 :(得分:1)

这是在黑暗中拍摄的照片。不确定查询中的哪些列引用您提到的速记。但是使用条件聚合,您可以执行以下操作。

select Id
    , Insurer1 = max(case when PolCount = 1 then BinderInsurerName end)
    , Perc1 = max(case when PolCount = 1 then BinderInsurerPerc end)
    , Flag1 = max(case when PolCount = 1 then LeadInsurer end)
    , Insurer2 = max(case when PolCount = 2 then BinderInsurerName end)
    , Perc2 = max(case when PolCount = 2 then BinderInsurerPerc end)
    , Flag2 = max(case when PolCount = 2 then LeadInsurer end)
    , Insurer3 = max(case when PolCount = 3 then BinderInsurerName end)
    , Perc3 = max(case when PolCount = 3 then BinderInsurerPerc end)
    , Flag3 = max(case when PolCount = 4 then LeadInsurer end)
    , Insurer4 = max(case when PolCount = 4 then BinderInsurerName end)
    , Perc4 = max(case when PolCount = 4 then BinderInsurerPerc end)
    , Flag4 = max(case when PolCount = 4 then LeadInsurer end)    
    , Insurer5 = max(case when PolCount = 5 then BinderInsurerName end)
    , Perc5 = max(case when PolCount = 5 then BinderInsurerPerc end)
    , Flag5 = max(case when PolCount = 5 then LeadInsurer end)
from
(       
    SELECT Policy.Id,
        BinderInsurer.[Percentage] As BinderInsurerPerc,
        BinderInsurer.LeadInsurer,
        Account.RegisteredCompanyName As BinderInsurerName,
        ROW_NUMBER() OVER (PARTITION BY Policy.Id ORDER BY BinderInsurer.[Percentage] DESC) AS PolCount
    FROM [Policy]
    Left Join Scheme On Scheme.Id = [Policy].SchemeId
    Left Join BinderAgreement On BinderAgreement.Id = Scheme.BinderAgreementId
    Left Join BinderSection On BinderSection.Id = Scheme.BinderSectionId
    Left Join BinderPeriod On BinderPeriod.Id = Scheme.BinderPeriodId
    Left Join BinderInsurer On BinderAgreement.Id = BinderInsurer.BinderAgreementId
    Left Join Account On Account.Id = BinderInsurer.AccountId
) x
group by x.Id