动态数据透视中的行和列平均值

时间:2017-04-19 07:30:55

标签: sql sql-server sql-server-2005

在SQL Server 2008中,我有一个包含3列的表tblStock

PartCode (NVARCHAR (50))
StockQty (INT)
Location (NVARCHAR(50))

下面的一些示例数据:

enter image description here

我想得到以下输出,

enter image description here

我编写了以下查询来获取上述输出,Avg值未正确返回:

IF OBJECT_ID('tempdb..#tblStock') IS NOT NULL /*Then it exists*/ 
    DROP TABLE #tblStock 

SELECT * 
INTO #tblStock
FROM
    (SELECT 'A' PartCode,  10 StockQty, 'WHs-A' Location
     UNION ALL
     SELECT 'B',   22,  'WHs-A'
     UNION ALL
     SELECT 'A',   1,   'WHs-B'
     UNION ALL
     SELECT 'C',   20,  'WHs-A'
     UNION ALL
     SELECT 'D',   39,  'WHs-F'
     UNION ALL
     SELECT 'E',   3,   'WHs-D'
     UNION ALL
     SELECT 'F',   7,   'WHs-A'
     UNION ALL
     SELECT 'A',   9,   'WHs-C'
     UNION ALL
     SELECT 'D',   2,   'WHs-A'
     UNION ALL
     SELECT 'F',   54,  'WHs-E') TAB

SELECT * FROM #tblStock 

DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + Location + ']', '[' + Location + ']')
               FROM (SELECT DISTINCT Location FROM #tblStock) PV 
               ORDER BY Location 
-- Since we need Total in last column, we append it at last
SELECT @cols += ',[AVG]'

-- Variable to replace NULL with zero
DECLARE @NulltoZeroCols NVARCHAR (MAX)
SELECT @NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+Location+'],0) AS ['+Location+']' 
FROM (SELECT DISTINCT Location FROM #tblStock)TAB  
ORDER BY Location FOR XML PATH('')),2,8000) 
SELECT @NullToZeroCols += ',ISNULL([AVG],0) AS [AVG]'


DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT PartCode,' + @NulltoZeroCols + ' FROM 
             (
                 SELECT 
                 ISNULL(CAST(PartCode AS VARCHAR(30)),''AVG'')PartCode, 
                 AVG(StockQty) StockQty  , 
                 ISNULL(Location,''AVG'')Location              
                 FROM #tblStock
                 GROUP BY Location,PartCode
                 WITH CUBE
             ) x
             PIVOT 
             (
                 AVG(StockQty)
                 FOR Location IN (' + @cols + ')
            ) p
            ORDER BY CASE WHEN (PartCode=''AVG'') THEN 1 ELSE 0 END,PartCode' 

EXEC SP_EXECUTESQL @query 

有谁知道这个问题的解决方案?

1 个答案:

答案 0 :(得分:2)

我用这个改变你的SET查询获得你想要的结果。
我认为您需要添加RIGHT JOIN以列出每个PartCode和Location(否则AVG功能无法正确计算项目数)。我也将ISNULL添加到AVG(StockQty)。 此外,您应该将平均值转换为数字。

text = strings.TrimSpace(text)
if text == "" {
    fmt.Println("No input!")
} else {
    fmt.Println("Hello", text)
}

输出:

SET @query = 'SELECT PartCode,' + @NulltoZeroCols + ' FROM 
             (SELECT                    
                 ISNULL(CAST(X.PC AS VARCHAR(30)),''AVG'') PartCode, 
                 AVG(CAST(ISNULL(StockQty,0) AS NUMERIC(10,2))) StockQty,                  
                 ISNULL(Y.LC,''AVG'') Location              
                 FROM #tblStock
                 RIGHT JOIN (SELECT DISTINCT PartCode AS PC FROM #tblStock)  X
                          CROSS JOIN  (SELECT DISTINCT Location AS LC FROM #tblStock) Y
                  ON Y.LC = #tblStock.LOCATION AND X.PC=#tblStock.PARTCODE
                 GROUP BY  Y.LC, X.PC  
                 WITH CUBE                 
             ) x
             PIVOT 
             (
                 AVG(StockQty)
                 FOR Location IN (' + @cols + ')
            ) p
            ORDER BY CASE WHEN (PartCode=''AVG'') THEN 1 ELSE 0 END,PartCode'