标签:avg lead selected 正数 col str 第一条 code val
max()、min()、avg()、sum()、count()
SQL> select deptno,ename,sal, sum(sal) over(partition by deptno order by deptno) sum_sal, round(avg(sal) over(partition by deptno)) avg_sal, count(*) over(partition by deptno) count_deptno from emp; DEPTNO ENAME SAL SUM_SAL AVG_SAL COUNT_DEPTNO ---------- ---------- ---------- ---------- ---------- ------------ 10 CLARK 2450 8750 2917 3 10 KING 5000 8750 2917 3 10 MILLER 1300 8750 2917 3 20 JONES 2975 10875 2175 5 20 FORD 3000 10875 2175 5 20 ADAMS 1100 10875 2175 5 20 SMITH 800 10875 2175 5 20 SCOTT 3000 10875 2175 5 30 WARD 1250 9400 1567 6 30 TURNER 1500 9400 1567 6 30 ALLEN 1600 9400 1567 6 30 JAMES 950 9400 1567 6 30 BLAKE 2850 9400 1567 6 30 MARTIN 1250 9400 1567 6 14 rows selected.
first_value()和last_value()
获取第一条记录和后一条记录。
FIRST_VALUE (expr) OVER (analytic_clause)
例:将当前月份的销量除以前一个月份的销量,并将当前月份的销量除以下一个月份的销量。
select month, sum(amount) as mount_amount, sum(amount)/first_value(sum(amount) )over (order by month rows between 1 preceding and 1 following) as curr_div_prev, sum(amount)/last_value(sum(amount)) over (order by month rows between 1 preceding and 1 following) as curr_div_next from all_sales where year=2003 group by month order by month id month month_amount curr_div_prev curr_div_next -- ------- ----------------- ---------------- ------------------- 1 1 95525.55 1 0.818755807 2 2 116671.6 1.221365383 0.727796855 3 3 160307.92 1.374009785 0.910846665 4 4 175998.8 1.097879631 1.140261993 5 5 154349.44 0.876991434 1.235276191 6 6 124951.36 0.809535558 0.733729756 7 7 170296.16 1.362899611 0.800505867 8 8 212735.68 1.249210082 1.065758334 9 9 199609.68 0.93829902 0.754722791 10 10 264480.79 1.3249898 1.650714777 11 11 160221.98 0.605798175 1.166640806 12 12 137336.17 0.857161858 1
lag()和lead()函数
lag()和lead()函数可获得位于距当前记录指定距离处的那条记录中的数据。lead 和lag 的语法类似,以下以lag为例进行讲解。
lag(exp_str,offset,defval) over()
窗口函数
UNBOUNDED PRECEDING:表示窗口从分区的第一行开始。
UNBOUNDED FOLLOWING:表示窗口在分区的最后一行结束。
CURRENT ROW:
value_expr PRECEDING或value_expr FOLLOWING
物理偏移量和逻辑偏移量
如果指定ROWS:
如果指定RANGE:
标签:avg lead selected 正数 col str 第一条 code val
原文地址:https://www.cnblogs.com/marxist/p/12151136.html