Oracle_Count函数

COUNT

Syntax

Description of count.gif follows
Description of the illustration count.gif

See Also:

"Analytic Functions" for information on syntax, semantics, and restrictions

Purpose

COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function.

If you specify DISTINCT, then you can specify only the query_partition_clause of the analytic_clause. The order_by_clause and windowing_clause are not allowed.

If you specify expr, then COUNT returns the number of rows where expr is not null. You can count either all rows, or only distinct values of expr.

If you specify the asterisk (*), then this function returns all rows, including duplicates and nulls. COUNT never returns null.

See Also:

"About SQL Expressions" for information on valid forms of expr and "Aggregate Functions"

Aggregate Examples

The following examples use COUNT as an aggregate function:

SELECT COUNT(*) "Total"
  FROM employees;

     Total
----------
       107

SELECT COUNT(*) "Allstars"
  FROM employees
  WHERE commission_pct > 0;

 Allstars
---------
       35

SELECT COUNT(commission_pct) "Count"
  FROM employees;

     Count
----------
        35

SELECT COUNT(DISTINCT manager_id) "Managers"
  FROM employees;

  Managers
----------
        18

Analytic Example

The following example calculates, for each employee in the employees table, the moving count of employees earning salaries in the range 50 less than through 150 greater than the employee's salary.

SELECT last_name, salary,
       COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 50 PRECEDING AND
                      150 FOLLOWING) AS mov_count
  FROM employees
  ORDER BY salary, last_name;

LAST_NAME                     SALARY  MOV_COUNT
------------------------- ---------- ----------
Olson                           2100          3
Markle                          2200          2
Philtanker                      2200          2
Gee                             2400          8
Landry                          2400          8
Colmenares                      2500         10
Marlow                          2500         10
Patel                           2500         10

SQL> select comm from emp;

      COMM
----------

       300
       500

      1400




         0


      COMM
----------




14 rows selected.

SQL> select distinct comm from emp;

      COMM
----------

      1400
       500
       300
         0

5 rows selected.

SQL> select comm from emp;

      COMM
----------

       300
       500

      1400




         0


      COMM
----------




14 rows selected.

SQL> select distinct comm from emp;

      COMM
----------

      1400
       500
       300
         0

5 rows selected.

SQL> select count(comm) from emp where comm is not null;

COUNT(COMM)
-----------
          4

1 row selected.

SQL> select count(comm) from emp where comm is null;

COUNT(COMM)
-----------
          0

1 row selected.

总结:count(*)结果包含NULL行,count(某一列)不包含空行。


Oracle数据库中的count函数用于统计指定列或表中的行数。它可以返回符合指定条件的记录数,也可以统计某个列的不重复值的数量。在查询中,可以使用count函数来获取结果集的行数,或者统计某个列中非空值的数量。 例如,可以使用以下语句来统计表中满足某个条件的记录数: SELECT COUNT(*) FROM table_name WHERE condition; 其中,table_name是要统计的表名,condition是过滤条件。如果不需要过滤条件,可以省略WHERE子句。count函数还可以结合其他函数和操作符一起使用,以便进行更复杂的统计操作。 例如,可以使用以下语句来统计某个列的非空值的数量: SELECT COUNT(column_name) FROM table_name; 其中,column_name是要统计的列名。如果某个列中包含null值,并且不希望将null值计入统计结果,可以使用nvl函数将null值替换为其他值,然后再进行统计。 总之,count函数Oracle数据库中被广泛用于统计行数和计算某个列的非空值数量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [[Oracle数据库详解]](https://blog.csdn.net/qq_46824955/article/details/126957866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [oracle数据库常用分析函数与聚合函数的用法](https://blog.csdn.net/weixin_39773447/article/details/113215713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值