带有 MAX 的 SQL 数据透视表

Posted

技术标签:

【中文标题】带有 MAX 的 SQL 数据透视表【英文标题】:SQL Pivot Table with MAX 【发布时间】:2017-10-20 04:48:44 【问题描述】:

我被这个困住了。我开发了一个 SQL 脚本你获取这些信息:-

Result

这里是代码:-

SELECT [COST UNIT],[GL ACCOUNT]+ ' ' + [GL], SUM([CLOSING BALANCE]) AS [TOTAL GL CLOSING BALANCE] FROM 
(
    SELECT AB.StartDate AS [DATE], AB.FreeTextField_04 AS [COST UNIT], IT.GLAccountDistribution AS [GL ACCOUNT], GL.Description AS [GL], ST.ItemCode AS [ITEM CODE] , 
    ST.Description AS [DESCRIPTION],
    IT.UserField_03 AS PACKAGING, ST.StockOnHandUOM AS [U/MEA.], IT.CostPriceStandard AS [STD COST PRICE], ST.PhyStkTakeQty AS [STOCK QTY], 
    CAST(ROUND((ST.PhyStkTakeQty * IT.CostPriceStandard ),2) AS DECIMAL(12,2)) AS [CLOSING BALANCE]
    FROM [001].DBO.CS738281StkTake ST INNER JOIN [001].DBO.Absences AB ON ST.ReqID = AB.ID 
    INNER JOIN [200].DBO.Items IT ON ST.ItemCode = IT.ItemCode
    INNER JOIN [200].DBO.GRV_GLAccounts GL ON IT.GLAccountDistribution = GL.GLAccount
    WHERE AB.StartDate = '2017-08-31 0:00:00.000'
    --AND AB.FreeTextField_02 in ( '1CTY','bsp')
    AND IT.GLAccountDistribution IN (12010010,12010020,12010030,12010040,12010041,12010042,12010043,12010050,12010060,12010080,12030010)
    --ORDER BY [GL ACCOUNT],[ITEM CODE]
) tmp
GROUP BY [GL ACCOUNT], [GL], [COST UNIT]
ORDER BY [COST UNIT],[GL ACCOUNT]

我的目标是让数据透视表看起来像这样:- Required Result

我尝试了很多次,但无法正常工作。任何人都可以帮忙吗?非常感谢:)

【问题讨论】:

【参考方案1】:

PIVOT 表运算符与MAX 一起使用,如下所示:

WITH CTE
AS
(
    SELECT [COST UNIT],[GL ACCOUNT]+ ' ' + [GL], SUM([CLOSING BALANCE]) AS [TOTAL GL CLOSING BALANCE] FROM 
    (


         SELECT AB.StartDate AS [DATE], AB.FreeTextField_04 AS [COST UNIT], IT.GLAccountDistribution AS [GL ACCOUNT], GL.Description AS [GL], ST.ItemCode AS [ITEM CODE] , 
            ST.Description AS [DESCRIPTION],
            IT.UserField_03 AS PACKAGING, ST.StockOnHandUOM AS [U/MEA.], IT.CostPriceStandard AS [STD COST PRICE], ST.PhyStkTakeQty AS [STOCK QTY], 
            CAST(ROUND((ST.PhyStkTakeQty * IT.CostPriceStandard ),2) AS DECIMAL(12,2)) AS [CLOSING BALANCE]
            FROM [001].DBO.CS738281StkTake ST INNER JOIN [001].DBO.Absences AB ON ST.ReqID = AB.ID 
            INNER JOIN [200].DBO.Items IT ON ST.ItemCode = IT.ItemCode
            INNER JOIN [200].DBO.GRV_GLAccounts GL ON IT.GLAccountDistribution = GL.GLAccount
            WHERE AB.StartDate = '2017-08-31 0:00:00.000'
            --AND AB.FreeTextField_02 in ( '1CTY','bsp')
            AND IT.GLAccountDistribution IN (12010010,12010020,12010030,12010040,12010041,12010042,12010043,12010050,12010060,12010080,12030010)
            --ORDER BY [GL ACCOUNT],[ITEM CODE]

    ) tmp
    GROUP BY [GL ACCOUNT], [GL], [COST UNIT]
)
SELECT *
FROM
(
    SELECT GL, [COST UNIT], [TOTAL GL CLOSING BALANCE]
    FROM CTE 
) AS t
PIVOT
(
  MAX([TOTAL GL CLOSING BALANCE])
  FOR [COST UNIT] IN([1CTY], [AMCP], [AMPO])
) AS p

【讨论】:

您好 Dimitri,感谢您的反馈。我试过你的脚本,它返回一个错误:- Msg 8155, Level 16, State 2, Line 1 No column name is specified for column 2 of 'MyCTE'.

以上是关于带有 MAX 的 SQL 数据透视表的主要内容,如果未能解决你的问题,请参考以下文章

带有文本值的 SQL Server 数据透视表

SQL - BigQuery - 在多个列中使用 Group 和 MAX - 类似于数据透视表

MS SQL Server 中的动态数据透视

实战 SQL:销售数据的小计/合计/总计以及数据透视表

如屏幕截图中所述,我想在 SQL 中创建一个数据透视表 [重复]

从 SQL Server DB 更新单独工作表中的数据后自动刷新 Excel 2007 数据透视表