63.Oracle数据库SQL开发之 高级查询——使用报表函数
欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\
报表函数可用于执行跨越分组和组内分区的计算。
报表计算:SUM,AVG,MAX,MIN,COUNT,VARIANCE,STDDEV。可以用RATIO_TO_REPORT函数计算某个值在一组值的总和中所占的比率。
1. 总计报表
store@PDB1> selectmonth,prd_type_id,sum(sum(amount)) over (partition by month) as total_month_amount,sum(sum(amount))over (partition by prd_type_id) as total_product_type_amount from all_saleswhere year=2003 and month<=3 group by month,prd_type_id order bymonth,prd_type_id;
MONTH PRD_TYPE_ID TOTAL_MONTH_AMOUNT TOTAL_PRODUCT_TYPE_AMOUNT
---------- ----------- -------------------------------------------
1 1 95525.55 201303.92
1 2 95525.55 44503.92
1 3 95525.55 61003.92
1 4 95525.55 65693.31
1 5 95525.55
2 1 116671.6 201303.92
2 2 116671.6 44503.92
2 3 116671.6 61003.92
2 4 116671.6 65693.31
2 5 116671.6
3 1 160307.92 201303.92
3 2 160307.92 44503.92
3 3 160307.92 61003.92
3 4 160307.92 65693.31
3 5 160307.92
15 rows selected.
SUM(amount)计算一个销量总和。外部的SUM计算总计。
OVER(PARTITION BY month)让外部的SUM计算每一个月的总计。
2. 使用RATIO_TO_REPORT函数
使用RATIO_TO_REPORT函数可以用来计算某个值在一组值的总和中所占的比率。
store@PDB1> select month,prd_type_id,sum(amount)as prd_type_amount,ratio_to_report(sum(amount)) over (partition by month) asprd_type_ratio from all_sales where year=2003 and month<=3 group bymonth,prd_type_id order by month,prd_type_id;
MONTH PRD_TYPE_ID PRD_TYPE_AMOUNT PRD_TYPE_RATIO
---------- ----------- -----------------------------
1 1 38909.04 .40731553
1 2 14309.04 .149792804
1 3 24909.04 .260757881
1 4 17398.43 .182133785
1 5
2 1 70567.9 .604842138
2 2 13367.9 .114577155
2 3 15467.9 .132576394
2 4 17267.9 .148004313
2 5
3 1 91826.98 .57281624
3 2 16826.98 .104966617
3 3 20626.98 .128670998
3 4 31026.98 .193546145
3 5
15 rows selected.
比例的计算是通过将每一种产品的总量除以所有产品的总量得出的。