警告:消除空值

时间:2016-12-16 15:51:29

标签: sql sql-server

我有临时表(见图),列数事故和可预防事故数量应该显示数据,但是它们都显示为全NULL。我如何解决它?谢谢

enter image description here

1 个答案:

答案 0 :(得分:0)

原因是因为您正在使用空值进行聚合

让我用一个例子解释如下:

create TABLE #test (ID INT, Qty INT, Price MONEY)
INSERT INTO #test (ID, Qty, Price) SELECT 1, 25, 100
INSERT INTO #test (ID, Qty, Price) SELECT 2, NULL, 200
INSERT INTO #test (ID, Qty, Price) SELECT 3, 5, NULL

SELECT SUM(Qty)  FROM #test WHERE ID IN (1,3)
-- no warning
--Because qty in id 1,3 is not null

SELECT SUM(Price) FROM #test WHERE ID IN (1,3)
-- Warning: Null value is eliminated by an aggregate or other SET operation.
-- Because price 100 is added with NULL

SELECT AVG(Qty)  FROM #test WHERE ID IN (1,2)
-- Warning: Null value is eliminated by an aggregate or other SET operation.
-- Because qty is null + 25 to get average