sum(1)相当于count(*),如果你查询的满足条件的有五条数据
sum(0)是0条,
sum(1)就是5,
如果有sum(2)就是10。
select sum(1) from emp。
是在emp的每一行有一个字段和值都为1的常量。
所以sum(1)就是1*count(*),sum(2)就是2*count(*)
按照sales_id order by排序计算递加的销售总额
select sales_id,sales,dest,dept,revenue,sum(revenue)over(order by sales)递加销售总额 from test;
count(*) over()---在什么条件之上 统计总数
count(distinct id) over() ---在什么条件之上 统计唯一id的总数量
select sales_id,sales,count(*)over()求总计数,
count(*)over(order by sales_id)递加求计数,
count(*)over(partition by sales_id)分组求计数,
count(*)over(partition by sales_id order by sales)分组递加求计数
from test
count(*)和count(1)最大的区别有:
1、count(*)会带来全表扫描(效率低)
2、count(*)不会过滤掉一整行值为null的行
3、count(1)会过滤掉一整行为null的行 4、count(1)和count(主键) 这两个只扫描主键Index就可以得到数据,
或者说count(ROWID)这也是只扫描Index的(效率高)