Oracle開(kāi)發(fā)之報(bào)表函數(shù)
一、回顧一下前面《Oracle開(kāi)發(fā)之窗口函數(shù)》中關(guān)于全統(tǒng)計(jì)一節(jié),我們使用了Oracle提供的:
來(lái)統(tǒng)計(jì)全年的訂單總額,這個(gè)函數(shù)會(huì)在記錄集形成的過(guò)程中,每檢索一條記錄就執(zhí)行一次,它總共執(zhí)行了12次。這是非常費(fèi)時(shí)的。實(shí)際上我們還有更簡(jiǎn)便的方法:
sum(tot_sales) month_sales,
sum(sum(tot_sales)) over(order by month
rows between unbounded preceding and unbounded following) win_sales,
sum(sum(tot_sales)) over() rpt_sales
from orders
group by month;
MONTH MONTH_SALES WINDOW_SALES REPORT_SALES
---------- ----------- ------------ ------------
1 610697 6307766 6307766
2 428676 6307766 6307766
3 637031 6307766 6307766
4 541146 6307766 6307766
5 592935 6307766 6307766
6 501485 6307766 6307766
7 606914 6307766 6307766
8 460520 6307766 6307766
9 392898 6307766 6307766
10 510117 6307766 6307766
11 532889 6307766 6307766
12 492458 6307766 6307766
已選擇12行。
over函數(shù)的空括號(hào)表示該記錄集的所有記錄都應(yīng)該被列入統(tǒng)計(jì)的范圍,如果使用了partition by則先分區(qū),再依次統(tǒng)計(jì)各個(gè)分區(qū)。
二、RATIO_TO_REPORT函數(shù):
報(bào)表函數(shù)特(窗口函數(shù))特別適合于報(bào)表中需要同時(shí)顯示詳細(xì)數(shù)據(jù)和統(tǒng)計(jì)數(shù)據(jù)的情況。例如在銷售報(bào)告中經(jīng)常會(huì)出現(xiàn)這樣的需求:列出上一年度每個(gè)月的銷售總額、年底銷售額以及每個(gè)月的銷售額占全年總銷售額的比例:
方法①:
100 * round(cust_sales / region_sales, 2) || '%' Percent
from (select o.cust_nbr customer,
o.region_id region,
sum(o.tot_sales) cust_sales,
sum(sum(o.tot_sales)) over(partition by o.region_id) region_sales
from orders_tmp o
where o.year = 2001
group by o.region_id, o.cust_nbr) all_sales
where all_sales.cust_sales > all_sales.region_sales * 0.2;
這是一種笨方法也是最易懂的方法。
方法②:
sum(tot_sales) sp_sales,
round(sum(tot_sales) / sum(sum(tot_sales))
over (partition by region_id), 2) percent_of_region
from orders
where year = 2001
group by region_id, salesperson_id
order by region_id, salesperson_id;
方法③
sum(tot_sales) sp_sales,
round(ratio_to_report(sum(tot_sales))
over (partition by region_id), 2) sp_ratio
from orders
where year = 2001
group by region_id, salesperson_id
order by region_id, salesperson_id;
Oracle提供的Ratio_to_report函數(shù)允許我們計(jì)算每條記錄在其對(duì)應(yīng)記錄集或其子集中所占的比例。
以上就是Oracle報(bào)表函數(shù)用法的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Oracle 函數(shù)大全[字符串函數(shù),數(shù)學(xué)函數(shù),日期函數(shù)]
- oracle 存儲(chǔ)過(guò)程和函數(shù)例子
- ORACLE常用數(shù)值函數(shù)、轉(zhuǎn)換函數(shù)、字符串函數(shù)
- Oracle中instr函數(shù)使用方法
- 給Oracle添加split和splitstr函數(shù)的方法
- oracle to_char函數(shù)將number轉(zhuǎn)成string
- Oracle round()函數(shù)與trunc()函數(shù)區(qū)別介紹
- Oracle隨機(jī)函數(shù)之dbms_random使用詳解
- ORACLE時(shí)間函數(shù)(SYSDATE)深入理解
- oracle中的trim函數(shù)使用介紹
相關(guān)文章
oracle 實(shí)際值超過(guò)數(shù)據(jù)庫(kù)某個(gè)字段指定長(zhǎng)度報(bào)錯(cuò)解決
本節(jié)主要介紹了oracle 實(shí)際值超過(guò)數(shù)據(jù)庫(kù)某個(gè)字段指定長(zhǎng)度報(bào)錯(cuò)解決方法,需要的朋友可以參考下2014-07-07在客戶端配置TNS測(cè)試報(bào)錯(cuò)ORA-12170:TNS:連接超時(shí)
在Red Hat Enterprise Linux Server Releae 5.5 成功安裝ORACLE 10g 后,在客戶端配置TNS后,測(cè)試是否可以連接到數(shù)據(jù)塊服務(wù)器,結(jié)果報(bào)錯(cuò):ORA-12170:TNS:連接超時(shí)2012-12-12將oracle的create語(yǔ)句更改為alter語(yǔ)句使用
本文將詳細(xì)介紹oracle的create語(yǔ)句更改為alter語(yǔ)句使,需要了解更多的朋友可以參考下2012-11-11oracle數(shù)據(jù)庫(kù)id自增及生成uuid問(wèn)題
這篇文章主要介紹了oracle數(shù)據(jù)庫(kù)id自增及生成uuid問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05oracle 11g的安裝注意事項(xiàng)總結(jié)
這篇文章主要給大家介紹了關(guān)于oracle 11g的安裝注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Oracle過(guò)程與函數(shù)的區(qū)別分析
在Oracle數(shù)據(jù)庫(kù)中,過(guò)程和函數(shù)都以編譯后的形式存放在數(shù)據(jù)庫(kù)中,二者的主要區(qū)別在于他們的調(diào)用方式,下文對(duì)二者的區(qū)別作了詳盡的描述,供您參考2014-08-08Oracle連接出現(xiàn)ora-12154無(wú)法解析指定的連接標(biāo)識(shí)符
這篇文章主要介紹了Oracle連接出現(xiàn)ora-12154無(wú)法解析指定的連接標(biāo)識(shí)符,需要的朋友可以參考下2017-03-03Oracle關(guān)于重建索引爭(zhēng)論的總結(jié)
這篇文章主要介紹了Oracle關(guān)于重建索引爭(zhēng)論的總結(jié),本文總結(jié)了重建索引的理由、重建索引的本質(zhì)、反對(duì)重建索引的理由等內(nèi)容,需要的朋友可以參考下2014-09-09