如何通过旋转 T-SQL 中的其他列对列求和

How to sum column with pivoting other column in T-SQL

在下面的例子中,你如何总结 'Fee' 列并使 ControlNo 唯一?

代码示例:

IF OBJECT_ID('tempdb..#table1') IS NOT NULL 
    DROP TABLE #table1

CREATE TABLE #Table1 
(
     ControlNo INT, 
     Line varchar(50), 
     Profit INT, 
     Fee INT
)

INSERT INTO #Table1 (ControlNo, Line, Profit, Fee) 
VALUES (1111, 'Line1', 80, 30), 
       (1111, 'Line2', 100, 20), 
       (3333, 'Line1', 200, 50), 
       (4444, 'Line1', 50, 10), 
       (4444, 'Line2', 100, 40)

-- check
--select * from #Table1

SELECT * 
FROM #Table1
PIVOT
    (SUM(Profit)
        FOR Line IN ([Line1], [Line2])
    ) pvt
ORDER BY ControlNo

输出如下所示:

但需要看起来像这样:

ControlNo   Fee Line1   Line2
1111        50  80      100
3333        50  200     0
4444        50  50      100

更新:

遵循 Dale 的解决方案

我尽量模拟真实数据,但不知为何81的TerrPrem不见了?

IF OBJECT_ID('tempdb..#table1') IS NOT NULL DROP TABLE #table1

Create table #Table1 ( Guid uniqueidentifier, ControlNo int, Line varchar(50), Prem INT, TerrPrem int )

INSERT INTO #Table1 (Guid, ControlNo, Line, Prem, TerrPrem) 
    VALUES  ('169E54D8-F00A-43B8-9268-5DD3F5684C5A',4395768, 'Commercial General Liability',10987,0), 
            ('169E54D8-F00A-43B8-9268-5DD3F5684C5A',4395768, 'Commercial General Liability',81,81), 
            ('169E54D8-F00A-43B8-9268-5DD3F5684C5A',4395768, 'Contractors Pollution Liability',1013,0), 
            ('169E54D8-F00A-43B8-9268-5DD3F5684C5A',4395768, 'Contractors Pollution Liability',81,81)

-- check
--select * from #Table1



select * 
from #Table1
PIVOT(
SUM(Prem)
FOR Line IN ([Commercial General Liability],             
             [Contractors Pollution Liability])
             ) as PivotTable

为什么其中一个 TerrPrem 消失了?

                Guid                    ControlNo   TerrPrem    Commercial General Liability    Contractors Pollution Liability
169E54D8-F00A-43B8-9268-5DD3F5684C5A    4395768         0                   10987                           1013
169E54D8-F00A-43B8-9268-5DD3F5684C5A    4395768         81                    81                            81

对于只有 2 种线型,您最好使用 case 表达式

select [Guid], ControlNo, TerrPrem * count(*)
  , sum(case when Line = 'Commercial General Liability' then Prem else 0 end) [Commercial General Liability]
  , sum(case when Line = 'Contractors Pollution Liability' then Prem else 0 end) [Contractors Pollution Liability]
from #table1
group by [Guid], ControlNo, TerrPrem
order by [Guid], ControlNo, TerrPrem;

只是另一种选择...通过 CROSS APPLY 扩展行并将 [Fee] 添加到 PIVOT

Select ControlNo
      ,Fee   = IsNull(Fee,0)
      ,Line1 = IsNull(Line1,0)
      ,Line2 = IsNull(Line2,0)
 From (
        select ControlNo 
              ,B.*
         From #Table1 A
         Cross Apply ( values ('Fee',Fee)
                             ,(Line,Profit)
                     ) B(Item,Value)
      ) src
 Pivot ( sum(Value) for Item in ([Fee],[Line1],[Line2] ) ) pvt

结果

OR... 您可以使用条件聚合

Select ControlNo
      ,Fee   = sum(Fee)
      ,Line1 = sum( case when Line='Line1' then Profit else 0 end)
      ,Line2 = sum( case when Line='Line2' then Profit else 0 end)
 From  #Table1
 Group By ControlNo