赞
踩
SQL> desc orders; 名称 是否为空? 类型 ----------------------- -------- ---------------- MONTH NUMBER(2) TOT_SALES NUMBERSQL>
SQL> select * from orders; MONTH TOT_SALES---------- ---------- 1 610697 2 428676 3 637031 4 541146 5 592935 6 501485 7 606914 8 460520 9 392898 10 510117 11 532889 12 492458已选择12行。
回忆一下前面《Oracle开发专题之:分析函数(OVER)》一文中,我们使用了sum(sum(tot_sales)) over (partition by region_id) 来统计每个分区的订单总额。现在我们要统计的不单是每个分区,而是所有分区,partition by region_id在这里不起作用了。Oracle为这种情况提供了一个子句:rows between ... preceding and ... following。从字面上猜测它的意思是:在XXX之前和XXX之后的所有记录,实际情况如何让我们通过示例来验证:SQL> select month, 2 sum(tot_sales) month_sales, 3 sum(sum(tot_sales)) over (order by month 4 rows between unbounded preceding and unbounded following) total_sales 5 from orders 6 group by month; MONTH MONTH_SALES TOTAL_SALES---------- ----------- ----------- 1 610697 6307766 2 428676 6307766 3 637031 6307766 4 541146 6307766 5 592935 6307766 6 501485 6307766 7 606914 6307766 8 460520 6307766 9 392898 6307766 10 510117 6307766 11 532889 6307766 12 492458 6307766已选择12行。
SQL> select month, 2 sum(tot_sales) month_sales, 3 sum(sum(tot_sales)) over (order by month 4 rows between 1 preceding and unbounded following) all_sales 5 from orders 6 group by month; MONTH MONTH_SALES ALL_SALES---------- ----------- ---------- 1 610697 6307766 2 428676 6307766 3 637031 5697069 4 541146 5268393 5 592935 4631362 6 501485 4090216 7 606914 3497281 8 460520 2995796 9 392898 2388882 10 510117 1928362 11 532889 1535464 12 492458 1025347已选择12行。
SQL> select month, 2 sum(tot_sales) month_sales, 3 sum(sum(tot_sales)) over(order by month 4 rows between unbounded preceding and current row) current_total_sales 5 from orders 6 group by month; MONTH MONTH_SALES CURRENT_TOTAL_SALES---------- ----------- ------------------- 1 610697 610697 2 428676 1039373 3 637031 1676404 4 541146 2217550 5 592935 2810485 6 501485 3311970 7 606914 3918884 8 460520 4379404 9 392898 4772302 10 510117 5282419 11 532889 5815308 12 492458 6307766已选择12行。
SQL> select month, 2 sum(tot_sales) month_sales, 3 sum(sum(tot_sales)) over(order by month 4 rows between unbounded preceding and current row) current_total_sales, 5 sum(sum(tot_sales)) over(order by month 6 rows between unbounded preceding and unbounded following) total_sales 7 from orders 8 group by month; MONTH MONTH_SALES CURRENT_TOTAL_SALES TOTAL_SALES---------- ----------- ------------------- ----------- 1 610697 610697 6307766 2 428676 1039373 6307766 3 637031 1676404 6307766 4 541146 2217550 6307766 5 592935 2810485 6307766 6 501485 3311970 6307766 7 606914 3918884 6307766 8 460520 4379404 6307766 9 392898 4772302 6307766 10 510117 5282419 6307766 11 532889 5815308 6307766 12 492458 6307766 6307766已选择12行。
select trunc(order_dt) day, sum(sale_price) daily_sales, avg(sum(sale_price)) over (order by trunc(order_dt) range between interval '2' day preceding and interval '2' day following) five_day_avg from cust_order where sale_price is not null and order_dt between to_date('01-jul-2001','dd-mon-yyyy') and to_date('31-jul-2001','dd-mon-yyyy')
select month, first_value(sum(tot_sales)) over (order by month rows between 1 preceding and 1 following) prev_month, sum(tot_sales) monthly_sales, last_value(sum(tot_sales)) over (order by month rows between 1 preceding and 1 following) next_month, avg(sum(tot_sales)) over (order by month rows between 1 preceding and 1 following) rolling_avg from orders where year = 2001 and region_id = 6 group by month order by month;
select month, sum(tot_sales) monthly_sales, lag(sum(tot_sales), 1) over (order by month) prev_month_sales from orders where year = 2001 and region_id = 6 group by month order by month;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。