使用同样的sql,分别在mysql,oracle,hive中执行,对比执行结果
Mysql
create table temp(tat varchar(10));
insert into temp values(null),(‘111’),(‘222’),(‘333’),(‘’);
a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;
结果:
a: 5
b: 5
c: 4
d: 666
e: 166.5(相当于是666/4)
f: ‘’(空字符串)
g: 333
h: 333,222,111,’’,(null)
结论:
可以看出在mysql中,count(字段),sum(),avg(),min(),max()都会自动过滤null值,但是不会过滤‘’空字符串,空字符串这一行参与了计算;
Oracle
create table temp(tat varchar2(10));
insert into temp values(‘’);
insert into temp values(null);
insert into temp (‘111’);
insert into temp (‘222’);
insert into temp (‘333’);
a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;
结果:
a: 5
b: 5
c: 3
d: 666
e: 222
f: 111
g: 333
h: (null),(null),333,222,111
结论:oracle数据库中,‘’空字符串被存储为了null,在执行函数
count(字段),sum(),avg(),min(),max(),会过滤null值,然后进行计算;
hive
create table temp(tat string));
insert into temp values(null),(‘111’),(‘222’),(‘333’),(‘’);
a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;
结果:
a: 5
b: 5
c: 4
d: 666
e: 166.5
f: ‘’(空字符串)
g: 333
h: 333,222,111,’’,null
结论:
针对字段的函数计算count(字段),sum(),avg(),min(),max()会去除null值,但不会去除‘’值