如何将SQL Pivot空结果屏蔽为零

时间:2018-12-03 07:14:27

标签: sql sql-server

CREATE TABLE [dbo].[tblPresentStock](
    [sProductName] [nvarchar](100) NULL,
    [sStockistName] [nvarchar](100) NULL,
    [Qty] [int] NULL,
    [sDivision] [nvarchar](20) NULL
) ON [PRIMARY]

enter image description here

enter image description here

以下查询工作正常,但结果为空。

我想将Null结果替换为0。

/* COLUMNS HEADERS */
DECLARE @columnHeaders NVARCHAR (MAX)
SELECT @columnHeaders  = COALESCE (@columnHeaders   
+ ',[' + sStockistName + ']', '[' + sStockistName + ']')
FROM   TblPresentStock  
GROUP BY sStockistName
ORDER BY sStockistName


/* GRAND TOTAL COLUMN */
DECLARE @GrandTotalCol  NVARCHAR (MAX)
SELECT @GrandTotalCol = COALESCE (@GrandTotalCol + 'ISNULL ([' + 
CAST (sStockistName AS VARCHAR) +'],0) + ', 'ISNULL([' + CAST(sStockistName AS VARCHAR)+ '],0) + ')
FROM     TblPresentStock
  GROUP BY sStockistName
  ORDER BY sStockistName
 SET @GrandTotalCol = LEFT (@GrandTotalCol, LEN (@GrandTotalCol)-1)

/* GRAND TOTAL ROW */
DECLARE @GrandTotalRow  NVARCHAR(MAX)
SELECT @GrandTotalRow = COALESCE(@GrandTotalRow + ',ISNULL(SUM([' + 
CAST(sStockistName AS VARCHAR)+']),0)', 'ISNULL(SUM([' + CAST(sStockistName AS VARCHAR)+']),0)')
FROM     TblPresentStock
    GROUP BY sStockistName
  ORDER BY  sStockistName
----------------------------------------------

 -- DROP TABLE  temp_MatchesTotal

/* MAIN QUERY */
DECLARE @FinalQuery NVARCHAR (MAX)
SET @FinalQuery =   'SELECT *, (' + @GrandTotalCol + ') 
AS [Grand Total] INTO  #temp_MatchesTotal
            FROM
                (SELECT sProductName,sDivision,sStockistName,Qty
                    FROM  TblPresentStock
                ) A
            PIVOT
                (
                 sum (Qty)
                 FOR sStockistName
                 IN ('  +@columnHeaders +  ')
                ) B
ORDER BY sProductName,sDivision
SELECT * FROM  #temp_MatchesTotal UNION ALL
SELECT ''Grand Total'','''','+@GrandTotalRow +',  
ISNULL (SUM([Grand Total]),0) FROM  #temp_MatchesTotal
  DROP TABLE  #temp_MatchesTotal'
 -- PRINT 'Pivot Query '+@FinalQuery
 -- SELECT @FinalQuery
 EXECUTE(@FinalQuery)

enter image description here

任何建议都会很有帮助。


  

@Sixthsense您能只显示GrandTotalCol字符串吗?一世   想看看那里有什么确切的SQL版本。 – gotqn

行和列总计查询

SELECT COALESCE(',ISNULL(SUM([' + 
CAST(sStockistName AS VARCHAR)+']),0)', 'ISNULL(SUM([' + CAST(sStockistName AS VARCHAR)+']),0)')
as 'Row Grand Total Result ',

COALESCE ('ISNULL ([' + 
CAST (sStockistName AS VARCHAR) +'],0) + ', 'ISNULL([' + CAST(sStockistName AS VARCHAR)+ '],0) + ')
as 'Column Grand Total Result'

FROM TblPresentStock

上述查询的结果

enter image description here

2 个答案:

答案 0 :(得分:0)

最终输出控制在最外面的select子句中实现。例如

SELECT COALESCE(some_number,0) AS x

在数据透视查询中没有什么不同,除了现在的数据透视查询正在生成新的列名。您必须将这些新列名称中的每一个列出到最外面的select子句中,并在其中的每一个上使用COALESCE()(或ISNULL)。例如

SET @FinalQuery =   'SELECT *
  , COALESCE(' + GrandTotalCol1 + ',0) 
  , COALESCE(' + GrandTotalCol2 + ',0) 
...
  , COALESCE(' + GrandTotalCol12 + ',0) 
...

对于动态产生的数据透视SQL,这可能需要2组所产生的列的字符串,一组用于IN(....),另一组用于最外层的select子句。

答案 1 :(得分:0)

空值仅出现在查询结果中,但在gridview中应用时,它显示为空白(按预期)。

这有多愚蠢...:)