由于Oracle在count()的时候,并不计算null(空值)部分,所以和空值结合起来进行统计的时候还是有一些意思。
count这个函数应用的太频繁,有必要深究一番。
测试表格及其数据:
SQL> desc student;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
SID INTEGER Y
NAME VARCHAR2(10) Y
SQL> select * from student;
SID NAME
--------------------------------------- ----------
1 lzf
2 wth
SQL> desc subject
Name Type Nullable Default Comments
------- ------------ -------- ------- --------
SID INTEGER Y
SUBNAME VARCHAR2(20) Y
SQL> select * from subject;
SID SUBNAME
--------------------------------------- --------------------
1 chinese
1
1
2 chinese
2 math
1) count(*) 和count(?)
*表示的是计算记录数目,?表示的是计算具体某个字段的数目( 但不包含空值 ).
--计算 每个学生有关的记录数的多少 (*表示记录数)
SQL> Select a.sid,a.Name,Count(*)
2 From student a,subject b
3 Where b.sid=a.sid
4 Group By a.sid,a.Name
5 /
SID NAME COUNT(*)
--------------------------------------- ---------- ----------
1 lzf 3
2 wth 2
2)count和null(空值)
count(?) ,即计算某个字段的时候,并不计算空值。
--计算 每个学生的学科数目 ,这里用到count(?)。
SQL> Select a.sid,a.Name,Count(b.subname)
2 From student a,subject b
3 Where b.sid=a.sid
4 Group By a.sid,a.Name
5 /
SID NAME COUNT(B.SUBNAME)
--------------------------------------- ---------- ----------------
1 lzf 1
2 wth 2
可以看到,使用count(?)的时候,空值并不计算。
因为空值并不计算,所以有的时候,可以把不需要计算的内容设置为空值以避免计算
例如select a.sid,count(case when b.subname='englishi' then null else b.subname end) from xxxx.
3)count和distinct
很多时候,不希望重复计算重复出现的信息。
SQL> select * from score;
SID SUBNAME CJ
--------------------------------------- -------------------- ----------
1 chinese 100
1 chinese 80
1 chinese 60
1 chinese 70
2 math 70
2 math 80
2 math 100
2 english 67
8 rows selected
SQL> select sid,sum(cj),count(distinct subname) from score group by sid;
SID SUM(CJ) COUNT(DISTINCT SUBNAME)
--------------------------------------- ---------- ----------------------
1 310 1
2 317 2
上面这个例子用于 统计每个学生的总成绩,以及科目的数目 。 可以看出来distinct和count,sum之类的很好的地结合在一起了。
之所以能很好结合在一起,是因为执行计划值这样的:
a)扫描每条满足条件的记录.
b)对结果排序.
c)的在排序结果上可以通知做sum,distinct操作
d)最后才是做count(distinct)操作. 因为count是最外层的,所以并不影响结果.
4) coun(*) 和count(1)或者是count(常量)
此三者是等价的(或者是一样结果的),都是表示计算记录的数目。不存在谁比谁快的问题。
count这个函数应用的太频繁,有必要深究一番。
测试表格及其数据:
SQL> desc student;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
SID INTEGER Y
NAME VARCHAR2(10) Y
SQL> select * from student;
SID NAME
--------------------------------------- ----------
1 lzf
2 wth
SQL> desc subject
Name Type Nullable Default Comments
------- ------------ -------- ------- --------
SID INTEGER Y
SUBNAME VARCHAR2(20) Y
SQL> select * from subject;
SID SUBNAME
--------------------------------------- --------------------
1 chinese
1
1
2 chinese
2 math
1) count(*) 和count(?)
*表示的是计算记录数目,?表示的是计算具体某个字段的数目( 但不包含空值 ).
--计算 每个学生有关的记录数的多少 (*表示记录数)
SQL> Select a.sid,a.Name,Count(*)
2 From student a,subject b
3 Where b.sid=a.sid
4 Group By a.sid,a.Name
5 /
SID NAME COUNT(*)
--------------------------------------- ---------- ----------
1 lzf 3
2 wth 2
2)count和null(空值)
count(?) ,即计算某个字段的时候,并不计算空值。
--计算 每个学生的学科数目 ,这里用到count(?)。
SQL> Select a.sid,a.Name,Count(b.subname)
2 From student a,subject b
3 Where b.sid=a.sid
4 Group By a.sid,a.Name
5 /
SID NAME COUNT(B.SUBNAME)
--------------------------------------- ---------- ----------------
1 lzf 1
2 wth 2
可以看到,使用count(?)的时候,空值并不计算。
因为空值并不计算,所以有的时候,可以把不需要计算的内容设置为空值以避免计算
例如select a.sid,count(case when b.subname='englishi' then null else b.subname end) from xxxx.
3)count和distinct
很多时候,不希望重复计算重复出现的信息。
SQL> select * from score;
SID SUBNAME CJ
--------------------------------------- -------------------- ----------
1 chinese 100
1 chinese 80
1 chinese 60
1 chinese 70
2 math 70
2 math 80
2 math 100
2 english 67
8 rows selected
SQL> select sid,sum(cj),count(distinct subname) from score group by sid;
SID SUM(CJ) COUNT(DISTINCT SUBNAME)
--------------------------------------- ---------- ----------------------
1 310 1
2 317 2
上面这个例子用于 统计每个学生的总成绩,以及科目的数目 。 可以看出来distinct和count,sum之类的很好的地结合在一起了。
之所以能很好结合在一起,是因为执行计划值这样的:
a)扫描每条满足条件的记录.
b)对结果排序.
c)的在排序结果上可以通知做sum,distinct操作
d)最后才是做count(distinct)操作. 因为count是最外层的,所以并不影响结果.
4) coun(*) 和count(1)或者是count(常量)
此三者是等价的(或者是一样结果的),都是表示计算记录的数目。不存在谁比谁快的问题。