ORACLE--COUNT()函数使用

来源:互联网 发布:知乎live可以搜索吗 编辑:程序博客网 时间:2024/04/27 15:27

Oracle中count()函数需要注意的地方:

 

count(*)将返回表格中所有存在的行的总数包括值为null的行,然而count(列名)将返回表格中除去null以外的所有行的总数.
distinct+ 列名,得到的结果将是除去值为null后的结果,所以count(distinct+列名)得到的结果和count(列名)相同。

----------------------------------------------------------------------------------------------------------------
举例演示如下:


SQL> create table test
  2  (
  3  ename varchar2(10),
  4  sal number(4)
  5  );

表已创建。

SQL> insert into test values('fxe1',90);

已创建 1 行。

SQL> insert into test(ename) values('fxe2');

已创建 1 行。

SQL> insert into test(ename) values('fxe3');

已创建 1 行。

SQL> insert into test(ename) values('fxe4');

已创建 1 行。

SQL> insert into test values('fxe5',80);

已创建 1 行。

SQL> select * from test;

ENAME             SAL                                                           
---------- ----------                                                           
fxe1               90                                                           
fxe2                                                                            
fxe3                                                                            
fxe4                                                                            
fxe5               80                                                          

SQL> select count(*) from test;

  COUNT(*)                                                                      
----------                                                                      
         5                                                                     

SQL> select count(sal) from test;

COUNT(SAL)                                                                      
----------                                                                      
         2                                                                     


SQL> select count(distinct sal) from test;

COUNT(DISTINCTSAL)                                                              
------------------                                                              
                 2                                                             

 

SQL> select distinct sal from test;

       SAL
----------
        80
        90


SQL> select count(distinct *) from test;
select count(distinct *) from test
                      *
ERROR 位于第 1 行: 
ORA-00936: 缺少表达式


SQL> spool off

0 0