用来返回查询的行数。
当指定distinct时,不能接order_by_clause;
如果指定表达式,count返回表达式不为空的值;
当指定*号时,它返回所有行,含重复行和空值。count从不返回null;
count(*) 与 count(1) 执行结果相同,但1并不是指第一列。列名为参数时,查的是不含NULL值的行数;
Aggregate Examples (聚合)
-- 含重复行和空值,所有行
SELECT COUNT(*) "Total" FROM employees;
SELECT COUNT(1) "Total" FROM employees;
Total
----------
107
-- 接条件
SELECT COUNT(*) "Allstars" FROM employees
WHERE commission_pct > 0;
Allstars
---------
35
-- commission_pct 不为空,但有重复行
SELECT COUNT(commission_pct) "Count" FROM employees;
Count
----------
35
-- 不含重复行和空值
SELECT COUNT(DISTINCT manager_id) "Managers" FROM employees;
Managers
----------
18
Analytic Example (分析)
SELECT last_name, salary,
COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 50 PRECEDING
AND 150 FOLLOWING) AS mov_count FROM employees;
LAST_NAME SALARY MOV_COUNT
------------------------- ---------- ----------
Olson 2100 3
Markle 2200 2
Philtanker 2200 2
Landry 2400 8
Gee 2400 8
Colmenares 2500 10
Patel 2500 10
参考:
[1]:https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm "oracle count"
[2]:http://sql.standout-dev.com/2017/01/count-vs-count1-again/ "Count(*) Vs Count(1) Again?"