distinct count

实验:查询一个column的无重复记录,需要知道有多少条记录,并显示记录。

统计记录用count(*)函数,无重复记录distinct,以emp表为例。

1)先查询无重复记录

[@more@]

SQL>select distinct emp.sal from scott.emp;

       SAL

----------

       800

       950

      1100

      1250

      1300

      1500

      1600

      2450

      2850

      2975

      3000

        SAL

----------

      5000

 已选择12行。

 2)查询合计记录数

SQL> select count(sal) from scott.emp;

 

COUNT(SAL)

----------

        14

 3)查询无重复记录数

SQL> select count(distinct emp.sal) from scott.emp;

 COUNT(DISTINCTEMP.SAL)

------------------

                12

4)同时使用distinctcount查询,报ORA—00937错误:非单组分组函数。需要与group by子句合用才可以。

SQL> select distinct sal ,count(*) from emp;
select distinct sal,count(*) from emp
                *
ERROR 位于第 1 行:
ORA-00937: 非单组分组函数

用命令查询显示不同记录、合计相同列数的记录:

SQL> select distinct emp.sal,count(*) from scott.emp group by sal;

        SAL   COUNT(*)

---------- ----------

       800          1

       950          1

      1100          1

      1250          2

      1300          1

      1500          1

      1600          1

      2450          1

      2850          1

      2975          1

      3000          2

        SAL   COUNT(*)

---------- ----------

      5000          1

 已选择12行。

 

分析原因:distinct返回是是多条记录;

          count(*)合计返回的是一条记录。

posted @ 2017-12-18 14:34  agang_19  阅读(759)  评论(0编辑  收藏  举报