0

我有这个带有透视数据的gridview:

    Store        MEX       USA
    Supermarket  10,000    5,000
    Club         10,000        0
    Direct        4,000    1,000

用这个查询完成:

    SELECT * 
    FROM [Sales] 
    PIVOT ( 
            SUM(Sales) 
            FOR Region IN ( [MEX], [USA] ) 
    ) AS SalesPerRegion

现在我想在页脚添加 MEX 和美国的总销售额,但我不知道该怎么做。

任何帮助表示赞赏,谢谢!

4

1 回答 1

1

您可以添加一个以获得每个区域UNION ALL的总计SUM()

SELECT Store, MEX, USA 
FROM [Sales] 
PIVOT ( 
        SUM(Sales) 
        FOR Region IN ( [MEX], [USA] ) 
) AS SalesPerRegion

UNION ALL

SELECT 'Total', Sum(MEX), Sum(USA)
FROM
(
    SELECT Store, MEX, USA 
    FROM [Sales] 
    PIVOT ( 
           SUM(Sales) 
           FOR Region IN ( [MEX], [USA] ) 
    ) AS SalesPerRegion
) x

请参阅带有演示的 SQL Fiddle

于 2012-08-16T20:17:27.387 回答