group by是对一组数据进行操作,这组数据可以理解为一个窗口
over是对每一条数据操作,但是依赖于此时的窗口
select deptno, count(*)
from emp
group by deptno;
输出结果:
select deptno, count(*) over()
from emp
group by deptno;
输出结果:
select deptno, count(*) over()
from emp;
输出结果:
通过这个例子,体会开窗函数的强大:
select deptno, count(*) over(partition by job)
from emp;
-- 可惜,下面这个demo根本不对,也不能运行
-- 等效于:
select deptno, count(*)
from emp
group by job;
输出结果:
理解一个移动平均:over()函数中的order会导致窗口滑动
select ename, sal, sum(sal) over(order by sal)
from emp
order by sal;
输出结果: