ITPub博客

首页 > Linux操作系统 > Linux操作系统 > count_sum_distinct与null

count_sum_distinct与null

原创 Linux操作系统 作者:wisdomone1 时间:2013-03-05 13:49:18 0 删除 编辑

SQL> create table t_agg(a int,b int,c int);
 
Table created
 
SQL> insert into t_agg values(1,null,1);
 
1 row inserted
 
SQL> insert into t_agg values(1,2,null);
 
1 row inserted
 
SQL> insert into t_agg values(2,1,null);
 
1 row inserted
 
SQL> insert into t_agg values(2,null,null);
 
1 row inserted
 
SQL> insert into t_agg values(3,4,8);
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_agg;
 
                                      A                                       B                                       C
--------------------------------------- --------------------------------------- ---------------------------------------
                                      1                                                                               1
                                      1                                       2
                                      2                                       1
                                      2                                        
                                      3                                       4                                       8
 
SQL> select count(b),count(c) from t_agg group by a;
 
  COUNT(B)   COUNT(C)
---------- ----------
         1          1
         1          0
         1          1
 
SQL> select a,count(b),count(c) from t_agg group by a;
 
                                      A   COUNT(B)   COUNT(C)
--------------------------------------- ---------- ----------
                                      1          1          1
                                      2          1          0
                                      3          1          1
 
SQL> truncate table t_agg;
 
Table truncated
 
SQL> insert into t_agg(a,b) values(1,null);
 
1 row inserted
 
SQL> insert into t_agg(a,b) values(1,1);
 
1 row inserted
 
SQL> insert into t_agg(a,b) values(1,1);
 
1 row inserted
 
SQL> insert into t_agg(a,b) values(2,1);
 
1 row inserted
 
SQL> insert into t_agg(a,b) values(2,2);
 
1 row inserted
 
SQL> insert into t_agg(a,b) values(3,1);
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_agg;
 
                                      A                                       B                                       C
--------------------------------------- --------------------------------------- ---------------------------------------
                                      1                                        
                                      1                                       1
                                      1                                       1
                                      2                                       1
                                      2                                       2
                                      3                                       1
 
6 rows selected
 
SQL> select a,count(distinct b) from t_agg group by a;
 
                                      A COUNT(DISTINCTB)
--------------------------------------- ----------------
                                      1                1
                                      2                2
                                      3                1
 
SQL> truncate table t_agg;
 
Table truncated
 
SQL> insert into t_agg(a) values(null);
 
1 row inserted
 
SQL> insert into t_agg(a) values(null);
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select count(a) from (select distinct a as a from t_agg);
 
  COUNT(A)
----------
         0

小结:count,sum,distinct忽略null,不计算在内

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9240380/viewspace-755305/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥

注册时间:2008-04-04

  • 博文量
    2189
  • 访问量
    12686897