SQL Server PIVOT - 多个聚合

Posted

技术标签:

【中文标题】SQL Server PIVOT - 多个聚合【英文标题】:SQL Server PIVOT - Multiple Aggregates 【发布时间】:2010-11-17 15:10:00 【问题描述】:

给定以下结果集:

---------------------------------------------------------
CustomerID  Service  TransType  SubTotal   Tax   NetTotal
---------------------------------------------------------
106            A        CREDIT     12.52     -      12.52 
106            A        CREDIT     10.07     -      10.07
106            B        CREDIT      2.00     -       2.00
106            C        REMOTE      5.99     -       5.99
106            C        CREDIT      5.99     -       5.99
106            C        CREDIT      3.99  0.30       3.69
106            C        CREDIT      5.99  0.30       5.69
106            D        CREDIT      5.99     -       5.99
---------------------------------------------------------

请注意,NetTotal = SubTotal - Tax

请帮我计算 sum(SubTotal)、sum(Tax) 和 sum(NetTotal),以及旋转的 TransType,如下:

--------------------------------------------------------------------------
CustomerID  Service  Cash  Check  Credit  Remote  SubTotal   Tax  NetTotal
--------------------------------------------------------------------------
106            A        0      0   22.59       0     22.59     0     22.59   
106            B        0      0    2.00       0      2.00     0      2.00    
106            C        0      0   15.97    5.99     21.96  0.60     21.36    
106            D        0      0    5.99       0      5.99     0      5.99    
--------------------------------------------------------------------------

如果我只有 1 列要汇总,那么使用 PIVOT 将是直接的,但我不确定如何获得 3 个聚合 - 用于 SubTotal、Tax 和 NetTotal。

感谢您的帮助!

【问题讨论】:

【参考方案1】:

这可以在没有 PIVOT 的情况下完成:

SELECT 
  CustomerID
, [Service]
, Cash = SUM(case when TransType='CASH' then SubTotal else 0 end)
, [Check] = SUM(case when TransType='CHECK' then SubTotal else 0 end)
, Credit = SUM(case when TransType='CREDIT' then SubTotal else 0 end)
, [Remote] = SUM(case when TransType='REMOTE' then SubTotal else 0 end)
, SubTotal = SUM(SubTotal)
, Tax = SUM(Tax)
, NetTotal = SUM(NetTotal)
FROM YourTable
GROUP BY CustomerId, [Service]

使用 PIVOT,它变得更加复杂。我能想到的最简单的方法是在不同的查询中计算 SubTotal、Tax 和 NetTotal,然后将查询与联接结合起来。下面的例子;为了使查询简单,我放弃了现金和支票。

SELECT  
  a.CustomerId
, a.Service
, Credit = a.Credit
, [Remote] = a.[Remote]
, SubTotal = SUM(b.SubTotal)
, Tax = SUM(b.Tax)
, NetTotal = SUM(b.NetTotal)
FROM (
    SELECT 
      CustomerId
    , [Service]
    , Credit = SUM(Credit)
    , [Remote] = SUM([Remote])
    FROM YourTable a
    PIVOT
    (
        SUM(SubTotal) FOR [TransType] IN ([Credit],[Remote])
    ) pvt
    GROUP BY CustomerId, [Service]
) a
INNER JOIN YourTable b 
    ON a.CustomerID = b.CustomerID 
    AND a.[Service] = b.[Service]
GROUP BY a.CustomerId, a.[Service], a.Credit, a.[Remote]

【讨论】:

谢谢安多玛!我限制自己使用 PIVOT,并监督了一个更简单的解决方案,比如你给我的那个。感谢您的帮助!【参考方案2】:

你尝试过这样的事情吗?

DECLARE @Table TABLE(
        CustomerID INT,
        [Service] VARCHAR(MAX),
        TransType VARCHAR(MAX),
        SubTotal FLOAT,
        Tax FLOAT,
        NetTotal FLOAT
)

INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'A', 'CREDIT', 12.52, 0 , 12.52
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'A', 'CREDIT', 10.07, 0 , 10.07
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'B', 'CREDIT', 2, 0 , 2
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'REMOTE', 5.99, 0 , 5.99
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 5.99, 0 , 5.99
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 3.99, 0.3 , 3.69
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 5.99, 0.3 , 5.69
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'D', 'CREDIT', 5.99, 0 , 5.99

SELECT  Pivots.CustomerID,
        Pivots.[Service],
        Pivots.Cash,
        Pivots.[Check],
        Pivots.Credit,
        Pivots.[Remote],
        Total.SumSubTotal,
        Total.SumTax,
        Total.SumNetTotal
FROM    (
            SELECT  CustomerID,
                    [Service],
                    [Cash] Cash,
                    [Check] [Check],
                    [Credit] Credit,
                    [Remote] [Remote]
            FROM    (
                        SELECT  CustomerID,
                                [Service],
                                TransType,
                                SubTotal
                        FROM    @Table
                    ) sub
                    PIVOT
                    (
                        SUM(SubTotal)
                        FOR [TransType] IN ([Cash],[Check],[Credit],[Remote])
                    ) pvt
            ) Pivots INNER JOIN
            (                       
                SELECT  CustomerID,
                        [Service],
                        SUM(SubTotal) SumSubTotal,
                        SUM(Tax) SumTax,
                        SUM(NetTotal) SumNetTotal
                FROM    @Table
                GROUP BY CustomerID,
                        [Service]
            ) Total ON  Pivots.CustomerID = Total.CustomerID
                    AND Pivots.[Service] = Total.[Service]

【讨论】:

谢谢你。我将另一个答案标记为正确,因为它以更简单的方式解决了问题。不过,我非常感谢您的回答。

以上是关于SQL Server PIVOT - 多个聚合的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server中行列转换 Pivot UnPivot

同一列上具有多个聚合的 T-SQL Pivot

请教SQL server 中pivot的详细用法及语法规则

SQL 连接,使用 Pivot 聚合

具有动态生成列、聚合函数和无聚合列的 SQL Pivot

在数据透视聚合函数中使用CONCAT函数的SQL Server错误