由于COUNT(column)
折扣空值,您可以使用:
SELECT CASE WHEN COUNT(*) = COUNT(num) THEN MAX(num) ELSE "OK" END
FROM Test;
我使用的 DBMS(Informix)要求 THEN 和 ELSE 中的类型相同,所以我最终测试了:
SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
FROM Test;
它是一种冗长的强制转换方式(但它是标准 SQL);在给定的 DBMS 中通常有可用的速记。
示例输出:
+ CREATE TABLE Test(Num INTEGER);
+ INSERT INTO Test VALUES(1);
+ INSERT INTO Test VALUES(2);
+ INSERT INTO Test VALUES(3);
+ INSERT INTO Test VALUES(NULL);
+ INSERT INTO Test VALUES(5);
+ INSERT INTO Test VALUES(6);
+ SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
FROM Test;
OK
+ DELETE FROM Test WHERE Num IS NULL;
+ SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
FROM Test;
6