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

警告:SQL中的聚合或其他集合操作会消除空值,sql,sql-server,tsql,isnull,Sql,Sql Server,Tsql,Isnull,很抱歉,在堆栈溢出上已经有很多问题了。 但是他们没有解决我关于空值的问题 我想从输出中消除空值 我已经在我的查询中使用了下面的技术,但问题仍然没有解决 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 n

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

我想从输出中消除空值

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

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
输出:


创建两个@cols列表。在一个示例中,在最终数据透视列中添加为空检查,如下所示:

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
                 '

这里

您想解决什么问题?我在你的帖子中没有看到问题?我想从output@AllanS.Hansen你收到我的查询了吗?使用coalesce或isnull函数之一将你的空值合并到域的有效元素。抱歉@Pieter Geerkens,但我没有使用coalesce函数,你能帮我吗?嘿,我还是没有得到我想要的输出受通缉的