从查询中获取结果的空值

时间:2017-01-30 16:01:28

标签: sql-server

我有这个问题:

SELECT U.LOCATION,SUM(S.EXISTENCIA) AS Total
FROM LOCATIONS U
LEFT JOIN CANT_WAREHOUSE S ON U.LOCATION= S.LOCATION
WHERE U.ENABLED= 1 
GROUP BY U.LOCATION

并返回这样的数据:

LOCATION  | Total
2A-1      | 566.0000000
2A-2      | 14.0000000
2A-3      | 1.0000000
2A-4      | (null)
2A-5      | 1.0000000
2A-6      | (null)
2A-7      | 6.0000000
2B-1      | 560.0000000
2B-2      | (null)

有没有办法获得“位置”'与' TOTAL' = null使用子查询或类似的东西?

2 个答案:

答案 0 :(得分:2)

使用HAVING条款

SELECT U.LOCATION,SUM(S.EXISTENCIA) AS Total 
FROM LOCATIONS U
LEFT JOIN CANT_WAREHOUSE S ON U.LOCATION= S.LOCATION
  and U.ENABLED= 1 
GROUP BY U.LOCATION
HAVING SUM(S.EXISTENCIA) IS NULL

答案 1 :(得分:1)

使用NOT EXISTS

SELECT U.LOCATION,
       NULL AS Total
FROM   LOCATIONS U
WHERE  NOT EXISTS (SELECT 1
                   FROM   CANT_WAREHOUSE S
                   WHERE  U.LOCATION = S.LOCATION)
       AND U.ENABLED = 1
GROUP  BY U.LOCATION