在SQL Server

时间:2016-10-16 06:56:15

标签: sql-server-2012

我发布了我的代码,通过使用pivot将行放到列中,但是我需要在pivot之后得到每一行的总和。

BEGIN
    SELECT @BranchName = BranchName
    FROM @t
    WHERE id = @i

    SELECT @columns = (
                        SELECT DISTINCT ','+QUOTENAME([SubInventory])
                        FROM #MyTempTable
                        WHERE [BranchName] = @BranchName
                        FOR XML PATH('')
                      )

    SELECT @sql = N'--
                SELECT   * 
                FROM (
                        SELECT [BranchID],
                               [SubInventory],
                               [Product],
                               [Stock]

                        FROM #MyTempTable
                        WHERE [BranchName] = ''' +@BranchName +''' 
                      ) as t
                PIVOT (

                        MAX([Stock]) FOR [SubInventory] IN ('+STUFF(@columns,1,1,'')+')
                      ) as pvt'

    EXEC sp_executesql @sql
    SET @i = @i+1

我附上了返回结果的格式,在这张图片中有两个表有不同的分支ID,所以我需要产品后列名的总和

Current Format

我要求的结果是

Required Format

当前屏幕

enter image description here

1 个答案:

答案 0 :(得分:3)

要评论大,请添加答案。

正如我在评论中提到的:如果您需要使用其他列中的sum添加一些新字段,则需要使用@column之类的数据添加另一个变量,例如Col1+Col2+Col3.. as [SUM(Col1-Col5)] s,然后使用{{1而不是...SELECT '+STUFF(@columns,1,1,'')+@sum +

我发布了您的查询的编辑版本。在SELECT *中,我们会存储@SUMcolumns,然后在动态部分中使用它:

Col1+Col2+Col3 as [sum]

修改

如果您使用的是SQL Server 2012及更高版本。您可以像这样使用SUM和AVG OVER PARTITION:

SELECT @SUMcolumns = ','+STUFF((
                    SELECT DISTINCT '+COALESCE('+QUOTENAME([SubInventory]) + ', 0)'
                    FROM #MyTempTable
                    WHERE [BranchName] = @BranchName
                    FOR XML PATH('')
                  ),1,1,'')+ ' as [sum]'


SELECT @sql = N'--
            SELECT  [BranchID],
                    [Product]'+
                    @columns+
                    @SUMcolumns
                    +'
            FROM (SELECT * FROM (
                    SELECT [BranchID],
                           [SubInventory],
                           [Product],
                           [Stock]

                    FROM #MyTempTable
                    WHERE [BranchName] = ''' +@BranchName +''' 
                  ) as t
            PIVOT (

                    MAX([Stock]) FOR [SubInventory] IN ('+STUFF(@columns,1,1,'')+')
                  ) as pvt
           ) as d'

这将为您带来如下输出:

DECLARE @SQL nvarchar(max),
        @columns nvarchar(max),
        @BranchName nvarchar(max) = 'Branch1'

SELECT @columns = (
                    SELECT DISTINCT ','+QUOTENAME([SubInventory])
                    FROM #MyTempTable
                    WHERE [BranchName] = @BranchName
                    FOR XML PATH('')
                    )

SELECT @sql = N'
SELECT  [BranchID],
        [Product]
        '+@columns+',
        [SUM Stock],
        [AVG Stock]
FROM (
        SELECT  [BranchID],
                [SubInventory],
                [Product],
                [Stock],
                SUM([Stock]) OVER (PARTITION BY [BranchID], Product) AS [SUM Stock],
                AVG([Stock]) OVER (PARTITION BY [BranchID], Product) AS [AVG Stock]
        FROM #MyTempTable
        WHERE [BranchName] = ''' +@BranchName +''' 
        ) as t
PIVOT (
    MAX([Stock]) FOR [SubInventory] IN ('+STUFF(@columns,1,1,'')+')
) as pvt'

EXEC sp_executesql @sql