Description

The Oracle/PLSQL COUNT function returns the count of an expression.

The COUNT(*) function

returns the number of rows in a table that satisfy the criteria of the SELECT statement,

including duplicate rows and rows containing null values in any of the columns.

If a WHERE clause is included in the SELECT statement,

COUNT(*) returns the number of rows that satisfy the condition in the WHERE clause.

COUNT(expr)

returns the number of non-null values that are in the column identified by expr.

COUNT(DISTINCT expr)

returns the number of unique, non-null values that are in the column identified by expr.

Syntax

SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];

OR

SELECT expression1, expression2, ... expression_n,
       COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

Parameters or Arguments

expression1, expression2, ... expression_n

Expressions that are not encapsulated within the COUNT function and must be included in the GROUP BY clause at the end of the SQL statement.

aggregate_expression

This is the column or expression whose non-null values will be counted.

Tables

The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

Optional. These are conditions that must be met for the records to be selected.

Only includes NOT NULL Values

Not everyone realizes this, but the COUNT function will only include the records in the count where the value of expression in COUNT(expression) is NOT NULL. When expression contains a NULL value, it is not included in the COUNT calculations.

With Single Field

SELECT COUNT(*) AS "Number of employees"
FROM employees
WHERE salary > 75000;

Using DISTINCT

SELECT COUNT(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 55000;

Using GROUP BY

SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE state = 'CA'
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the COUNT function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

使用DISTINCT 的方法COUNT函数和NVL函数的区别:

NVL

SELECT DISTINCT NVL(emp_name, 'AAA')
FROM employees;

COUNT

SELECT COUNT(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 55000;

Oracle Function:COUNT的更多相关文章

  1. Oracle Function: NVL

    Description The Oracle/PLSQL NVL function lets you substitute a value when a null value is encounter ...

  2. Oracle Function:TO_CHAR

    Description The Oracle/PLSQL TO_CHAR function converts a number or date to a string.将数字转换为日期或字符串 Syn ...

  3. oracle function学习1

    oracle function学习基层: 函数就是一个有返回值的过程.  首先 知道oracle 使用限制:      函数调用限制: 1. SQL语句中只能调用存储函数(服务器端),而不能调用客户端 ...

  4. PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr;  //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...

  5. Oracle维护:每天的工作

    Oracle维护:每天的工作 检查数据库状态 确认所有的INSTANCE状态以及listener状态正常,登陆到所有数据库或例程,检测ORACLE后台进程: $ ps –ef|grep ora $ l ...

  6. Oracle ORA-12541:TNS:无监听程序

    Oracle ORA-12541:TNS:无监听程序 标签: Oracle DataBase 今天使用Oracle数据库,使用可视化连接工具连接测试环境的数据库时提示无监听程序,最后在老师帮助下终于搞 ...

  7. 【转载】Oracle递归查询:使用prior实现树操作【本文出自叶德华博客】

    本文标题:Oracle递归查询:使用prior实现树操作 本文链接:http://yedward.net/?id=41 本文版权归作者所有,欢迎转载,转载请以文字链接的形式注明文章出处. Oracle ...

  8. Oracle错误:ORA-01033

    Oracle错误:ORA-01033 错误编码:ORA-01033: ORACLE initialization or shutdown in progress 故障描述:因为移动了数据库文件([NA ...

  9. lintcode :Count 1 in Binary 二进制中有多少个1

    题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...

随机推荐

  1. 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)

    (接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...

  2. 5 -- Hibernate的基本用法 --4 深入Hibernate配置文件

    Hibernate的持久化操作离不开SessionFactory对象,这个对象是整个数据库映射关系经过编译后的内存镜像,该对象的openSession()方法可打开Session对象.该对象通常由Co ...

  3. Java - Calendar类的使用

    今天在写代码时需要用到时间相关的类,一开始,数据库中存的数据类型是timestamp的,所以在Java中就使用了 Timestamp类型,但当调用Timestamp类型的方法时发现,它的很多方法都是d ...

  4. 利用Squid反向代理搭建CDN缓存服务器加快Web访问速度

    2011年11月26日 ? Web服务器架构 ? 评论数 2 案例:Web服务器:域名www.abc.com IP:192.168.21.129 电信单线路接入访问用户:电信宽带用户.移动宽带用户出现 ...

  5. 使用react进行父子组件传值

    在单页面里面,父子组件传值是比较常见的,之前一直用vue开发,今天研究了一下react的父子组件传值,和vue差不多的思路,父组件向子组件传值,父通过初始state,子组件通过this.props进行 ...

  6. 使用session防止表单进行重复提交

    我们都知道可以通过js的方法来实现防止表单重复提交,但是js只适用于“在网络延迟的情况下让用户有时间点击多次submit按钮导致表单重复提交” 的情况下进行操作, 那如果碰到“表单提交后用户点击[刷新 ...

  7. codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...

  8. Spring系列之访问数据库

    一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结合JDBC API的最佳实践和统一集成各种ORM方案,完成Java平台的数据访问. 二.JDBC API的最佳实践 Spring ...

  9. oracle闪回数据

    方法一 数据删除了: select * from  t_test  as of timestamp to_timestamp('2011-10-25 13:45:00','yyyy-mm-dd hh2 ...

  10. php 原生文件下载

    1.整个网页的html界面源码下载: xiazai.php <html> <head> <meta charset ="utf-8"> < ...