1

我正在运行同一查询的多个实例。下面的版本不应该返回任何数据,但我在每一列中都得到 1 行“null”:

SELECT (CASE ScanName WHEN 'SYSTEM-HQ' THEN 'HQ
System' END) AS System, 
sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 
3 THEN 6 WHEN 4 THEN 3 END) AS Score, 
(sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 
WHEN 3 THEN 6 WHEN 4 THEN 3 END)/COUNT(pspplMSSeverity)) AS 
Grade 
FROM missingpatches 
WHERE ScanName like '%SYSTEM-HQ%' 
ORDER BY LAST_UPDATE DESC LIMIT 1 

如何修改此查询以确保仅在存在有效值时才返回数据?

这可能是由于在导致返回 Null 数据的主 SQL 语句中使用了 Case 和 Sum?

4

2 回答 2

1
SELECT (CASE ScanName WHEN 'SYSTEM-HQ' THEN 'HQ
System' END) AS System, 
sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 
3 THEN 6 WHEN 4 THEN 3 END) AS Score, 
(sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 
WHEN 3 THEN 6 WHEN 4 THEN 3 END)/COUNT(pspplMSSeverity)) AS 
Grade 
FROM missingpatches 
WHERE ScanName like '%SYSTEM-HQ%' 
HAVING System IS NOT NULL # Added
ORDER BY LAST_UPDATE DESC LIMIT 1 

尝试添加 HAVING。

于 2012-05-24T15:09:01.690 回答
0

很可能该表missingpatches包含 1 行满足

WHERE ScanName like '%SYSTEM-HQ%' 

即 ScanName 包含“SYSTEM-HQ”但不完全等于“SYSTEM-HQ”的行,您在选择的第一列中等同于“SYSTEM-HQ”。

在 SQL 中,在连接表和评估 where 子句之后评估列。因此,您看到的行实际上符合 where 子句标准。

于 2012-05-24T15:13:45.663 回答