zoukankan      html  css  js  c++  java
  • 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(*)合计返回的是一条记录。

  • 相关阅读:
    腾讯2017暑期实习编程题3
    腾讯2017暑期实习编程题2
    腾讯2017暑期实习编程题1
    力扣算法题—098验证二叉搜索树
    题目1451:不容易系列之一
    题目1362:左旋转字符串(Move!Move!!Move!!!)
    HDU 2564 词组缩写
    HDU 2561 二小整数
    HDU 2034 人见人爱A-B
    HDU 1875 畅通工程再续
  • 原文地址:https://www.cnblogs.com/agang-php/p/8057478.html
Copyright © 2011-2022 走看看