无效此代码工作正常:操作数的数据类型NULL是最大运营商
select fk, max(case when 1 = 0 then 1 else null end) maxx
from (values(1, null), (1, null)) x(fk, a)
group by fk;
输出:
fk maxx
----------- -----------
1 NULL
有:
Warning: Null value is eliminated by an aggregate or other SET operation.
但这代码:
select fk, max(a) maxx
from (values(1, null), (1, null)) x(fk, a)
group by fk;
给予错误:
Msg 8117, Level 16, State 1, Line 5 Operand data type NULL is invalid for max operator.
在这两种情况下,SQL Server的计算从null
和null
max
?不是吗?
您可以通过显式将null转换为数据类型来进行第二个工作。 (1,CAST(NULL AS varchar(25))),(1,CAST(NULL AS varchar(25))))x(fk,a) group by fk;'当列的数据类型未知时,您无法获得最大值null。当你尝试创建一个只有空值的表时,你会注意到;它也会失败,因为数据类型是未知的。 – xQbert