警告:SQL中的聚合或其他SET操作消除了空值

时间:2014-07-15 05:25:48

标签: sql sql-server tsql isnull

很抱歉,堆栈溢出已经存在很多问题。 但他们没有解决我关于空值的问题。

我想从输出中消除 Null 值。

我已经在我的查询中使用了以下技术,但仍然没有解决问题。

SET ANSI_WARNINGS OFF;**
SUM(CASE WHEN [qty] IS NULL THEN 0 ELSE qty END) AS [qty]

这是我的查询

SET ANSI_WARNINGS ON;
DECLARE @cols nvarchar(max),
                        @query nvarchar(max),

                @Date DATETIME
                SET @Date = DATEADD(mm, -6, CURRENT_TIMESTAMP);

                WITH cte AS 
                        ( 
                        SELECT 0 AS TheMonth  
                        UNION ALL  
                        SELECT TheMonth + 1 
                        FROM cte 
                        WHERE TheMonth < 5 
                        )

                SELECT TheMonth
                INTO #temp
                FROM cte

SELECT @cols =   STUFF((SELECT distinct ',' + QUOTENAME(A.warehouse_name) 
                                 from RS_Company_Warehouse_Master a    
                                 FOR XML PATH(''), TYPE 
                                 ).value('.', 'NVARCHAR(MAX)') 
                                 ,1,1,'') 
SET ANSI_WARNINGS OFF;
SELECT @query = 'SELECT                  
                 LEFT(DATENAME(MONTH,DATEADD(MONTH,TheMonth -6 + 1,GETDATE())),3) AS [month], 
                 DATEPART(YEAR,DATEADD(MONTH,TheMonth - 6 + 1 ,GETDATE())) AS [year], 
                 '+@cols+'
                 from
                 (
                 SELECT 
                 TheMonth,                
                 SUM(CASE WHEN [qty] IS NULL THEN 0 ELSE qty END) AS [qty],
                 warehouse_name
                 FROM 
                 #temp
                 LEFT OUTER JOIN RS_Sell_Order_Master AS SM ON invoice_date >= DATEADD(MM, TheMonth, '''+convert(varchar(10), @Date, 120)+''')           
                                                            AND invoice_date < DATEADD(MM, TheMonth + 1, '''+convert(varchar(10), @Date, 120)+''')                                                                                                                                                
                 LEFT OUTER JOIN RS_Company_Warehouse_Master AS CWM ON CWM.c_warehouse_id = SM.c_warehouse_id
                 LEFT OUTER JOIN RS_Sells_Invoice_Info_Master AS SIIM ON SIIM.sell_order_no = SM.sell_order_no
                 GROUP BY 
                 CWM.warehouse_name,
                 TheMonth
                 ) as a
                 PIVOT
                 (
                    sum(qty) for warehouse_name
                    IN ('+@cols+')
                 ) AS P
                 '
                 execute sp_executesql @query;
                 DROP TABLE #temp

输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

创建两个@cols列表。在一个Add is null中,将最终的Pivot列检查为:

DECLARE @colsFinal nvarchar(max)
SET @colsFinal = N''
SELECT @colsFinal =  STUFF((SELECT distinct ',' +
                'ISNULL ( ''' + QUOTENAME(A.warehouse_name) +''', 0) as '
                + QUOTENAME(A.warehouse_name) 
                from RS_Company_Warehouse_Master a    
                FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')    

and change your main query as:

SELECT @query = 'SELECT                  
                 LEFT(DATENAME(MONTH,DATEADD(MONTH,TheMonth -6 + 1,GETDATE())),3) AS [month], 
                 DATEPART(YEAR,DATEADD(MONTH,TheMonth - 6 + 1 ,GETDATE())) AS [year], 
                 '+@colsFinal+' 
                 from
                 (
                 SELECT 
                 TheMonth,                
                 SUM(CASE WHEN [qty] IS NULL THEN 0 ELSE qty END) AS [qty],
                 warehouse_name
                 FROM 
                 #temp
                 LEFT OUTER JOIN RS_Sell_Order_Master AS SM ON invoice_date 
                 >= DATEADD(MM, TheMonth, '''+convert(varchar(10), @Date, 120)+''')           
                 AND invoice_date < DATEADD(MM, TheMonth + 1, '''
                 +convert(varchar(10), @Date, 120)+''')                                                                                                                                   
                 LEFT OUTER JOIN RS_Company_Warehouse_Master AS CWM ON CWM.c_warehouse_id = SM.c_warehouse_id
                 LEFT OUTER JOIN RS_Sells_Invoice_Info_Master AS SIIM ON SIIM.sell_order_no = SM.sell_order_no
                 GROUP BY 
                 CWM.warehouse_name,
                 TheMonth
                 ) as a
                 PIVOT
                 (
                    sum(qty) for warehouse_name
                    IN ('+@cols+')
                 ) AS P
                 '

SQL Fiddle Demo这里