不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
SQL Server2000聚合函数和空值
编辑:dnawo 日期:2010-04-10
SQL Server2000的聚合函数大都会忽略空值,所以在含有空值的列上使用聚合函数时需格外谨慎。例如有一个Student表如下:

我们用下边SQL语句统计下人数、平均年龄、最大年龄、最小年龄:
引用内容
可以看到,除了count(*),其他聚合函数都忽略了stu3。可以使用isnull函数给空值设置一个默认值,让聚合函数不忽略该行:
引用内容
注意:对avg、max、min聚合函数不应使用isnull,否则会出现用两个人的年龄计算三个人的平均年龄,这显然是不合理的。
很多时候,我们都会给字段设置一个默认值,当字段值为空值时就使用默认值,再看Student表:

我们用下边SQL语句统计下人数、平均年龄、最大年龄、最小年龄:
引用内容
很显然,avg、min的值不是我们想要的,avg和min都应忽略stu3,这时我们可以用nullif函数让聚合函数忽略它:
引用内容
说明:当以文本显示查询时,若对含空值的列使用聚合函数,SQL查询分析器会发出警告。
引用内容

我们用下边SQL语句统计下人数、平均年龄、最大年龄、最小年龄:
复制内容到剪贴板
程序代码

select
count(*) as count1,
count(age) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student
count(*) as count1,
count(age) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student

count1 count2 avg max min
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
可以看到,除了count(*),其他聚合函数都忽略了stu3。可以使用isnull函数给空值设置一个默认值,让聚合函数不忽略该行:
复制内容到剪贴板
程序代码

select
count(*) as count1,
count(isnull(age,0)) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student
count(*) as count1,
count(isnull(age,0)) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student

count1 count2 avg max min
----------- ----------- ----------- ----------- -----------
3 3 21 22 20
----------- ----------- ----------- ----------- -----------
3 3 21 22 20
注意:对avg、max、min聚合函数不应使用isnull,否则会出现用两个人的年龄计算三个人的平均年龄,这显然是不合理的。
很多时候,我们都会给字段设置一个默认值,当字段值为空值时就使用默认值,再看Student表:

我们用下边SQL语句统计下人数、平均年龄、最大年龄、最小年龄:
复制内容到剪贴板
程序代码

select
count(*) as count1,
count(age) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student
count(*) as count1,
count(age) as count2,
avg(age) as [avg],
max(age) as [max],
min(age) as [min]
from Student

count1 count2 avg max min
----------- ----------- ----------- ----------- -----------
3 3 14 22 0
----------- ----------- ----------- ----------- -----------
3 3 14 22 0
很显然,avg、min的值不是我们想要的,avg和min都应忽略stu3,这时我们可以用nullif函数让聚合函数忽略它:
复制内容到剪贴板
程序代码

select
count(*) as count1,
count(nullif(age,0)) as count2,
avg(nullif(age,0)) as [avg],
max(nullif(age,0)) as [max],
min(nullif(age,0)) as [min]
from Student
count(*) as count1,
count(nullif(age,0)) as count2,
avg(nullif(age,0)) as [avg],
max(nullif(age,0)) as [max],
min(nullif(age,0)) as [min]
from Student

count1 count2 avg max min
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
说明:当以文本显示查询时,若对含空值的列使用聚合函数,SQL查询分析器会发出警告。

count1 count2 avg max min
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
(所影响的行数为 1 行)
警告: 聚合或其它 SET 操作消除了空值。
----------- ----------- ----------- ----------- -----------
3 2 21 22 20
(所影响的行数为 1 行)
警告: 聚合或其它 SET 操作消除了空值。






评论: 0 | 引用: 0 | 查看次数: 4891
发表评论
请登录后再发表评论!