Oracle Reporting 1 - Ratio_to_Report Function
The RATIO_TO_REPORT function computes the ratio of a value to the sum of a set of values. RATIO_TO_REPORT ( expr ) OVER ( [query_partition_clause] ).
In this series of articles, I'm using Oracle's SH schema. It is modeled in star schema. The following query returns top 3 best-selling products in 2000, their sales amount, percentage of total amount, and the rank.
select * from (
SELECT prod.prod_name,
TO_CHAR(SUM(amount_sold),'9,999,999,999') "SALES($)",
to_char(RATIO_TO_REPORT(SUM(amount_sold)) OVER () * 100, '99.99' ) || '%' Percentage_of_Taotal_Amount,
rank() over(order by sum(amount_sold) desc) rank
FROM sales s, products prod
WHERE s.prod_id=prod.prod_id
AND trunc(s.time_id,'yyyy')=to_DATE('01-JAN-2000')
GROUP BY (prod.prod_name)
)
where rank <= 3;
Ratio_to_report() Performance Analysis:
Consider this query:
WITH channel_summary AS (SELECT channels.channel_desc, SUM(amount_sold)
AS channel_total FROM sales, channels
WHERE sales.channel_id = channels.channel_id GROUP BY channels.channel_desc)
SELECT channel_desc, channel_total
FROM channel_summary WHERE channel_total > (SELECT SUM(channel_total) * 1/3
FROM channel_summary);
Statistics
----------------------------------------------------------
2 recursive calls
8 db block gets
1730 consistent gets
1 physical reads
864 redo size
622 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
select channel_desc, amount_sold from
(select ch.channel_desc, sum(s.amount_sold) amount_sold, ratio_to_report(sum(s.amount_sold)) over() ratio from channels ch, sales s where ch.channel_id=s.channel_id group by ch.channel_desc)
where ratio >= 0.33;
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1721 consistent gets
0 physical reads
0 redo size
620 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
In this series of articles, I'm using Oracle's SH schema. It is modeled in star schema. The following query returns top 3 best-selling products in 2000, their sales amount, percentage of total amount, and the rank.
select * from (
SELECT prod.prod_name,
TO_CHAR(SUM(amount_sold),'9,999,999,999') "SALES($)",
to_char(RATIO_TO_REPORT(SUM(amount_sold)) OVER () * 100, '99.99' ) || '%' Percentage_of_Taotal_Amount,
rank() over(order by sum(amount_sold) desc) rank
FROM sales s, products prod
WHERE s.prod_id=prod.prod_id
AND trunc(s.time_id,'yyyy')=to_DATE('01-JAN-2000')
GROUP BY (prod.prod_name)
)
where rank <= 3;
Ratio_to_report() Performance Analysis:
Consider this query:
WITH channel_summary AS (SELECT channels.channel_desc, SUM(amount_sold)
AS channel_total FROM sales, channels
WHERE sales.channel_id = channels.channel_id GROUP BY channels.channel_desc)
SELECT channel_desc, channel_total
FROM channel_summary WHERE channel_total > (SELECT SUM(channel_total) * 1/3
FROM channel_summary);
Statistics
----------------------------------------------------------
2 recursive calls
8 db block gets
1730 consistent gets
1 physical reads
864 redo size
622 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Oracle needs to use temp tablespace to save the data of with-clause, that's the reason of 2 recursive calls and 864 redo size. However, we can eliminate the writes to/reads from temp tablespace by using ratio_to_report.
Both queries run twice to avoid the effect of hard-parse.
Both queries run twice to avoid the effect of hard-parse.
select channel_desc, amount_sold from
(select ch.channel_desc, sum(s.amount_sold) amount_sold, ratio_to_report(sum(s.amount_sold)) over() ratio from channels ch, sales s where ch.channel_id=s.channel_id group by ch.channel_desc)
where ratio >= 0.33;
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1721 consistent gets
0 physical reads
0 redo size
620 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-777446/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle分析函式之ratio_to_reportOracle函式
- Oracle Reporting 6 - ModelOracle
- Oracle Reporting 5 - WindowingOracle
- Oracle Reporting 7 - Model ExamplesOracle
- Oracle Reporting 2 - Subtotals and Grand TotalOracle
- Oracle Reporting 4 - Time Series CalculationsOracle
- oracle function overviewOracleFunctionView
- Oracle Table FunctionOracleFunction
- oracle function powerOracleFunction
- Oracle Reporting 3 - Aggregation LevelOracle
- oracle INTERNAL_FUNCTIONOracleFunction
- Oracle Deterministic FunctionOracleFunction
- Javascript 物件導向學習1 Function function ObjectJavaScript物件FunctionObject
- std::tr1::functionFunction
- Oracle Pipelined FunctionOracleFunction
- oracle function函式castOracleFunction函式AST
- BIRT呼叫oracle function薦OracleFunction
- a demo function of oracle for AKingFunctionOracle
- ABAP 常用FUNCTION集錦1Function
- Oracle基礎之function使用OracleFunction
- oracle create function 例項2OracleFunction
- Oracle Profile and PASSWORD_VERIFY_FUNCTIONOracleFunction
- Error reporting for dbusError
- 構建oracle function的小示例OracleFunction
- Oracle Date Function 講解和事例OracleFunction
- 'MessageBoxA' : function does not take 1 parameterFunction
- javascript 中function(){},new function(),new Function(),Function 摘錄JavaScriptFunction
- Oracle vs PostgreSQL Develop(28) - function overloadOracleSQLdevFunction
- oracle11中的char_functionOracleFunction
- Oracle replace function to delete sub-stringOracleFunctiondelete
- oracle create function的兩個例項OracleFunction
- Oracle Appliactions 11i concepts(十五) - Multiple Reporting CurrenciesOracleAPP
- error_reporting()的用法Error
- ORACLE HANDBOOK系列之一:Oracle分析函式(Analytic Function)Oracle函式Function
- Oracle vs PostgreSQL Develop(24) - subquery with agg functionOracleSQLdevFunction
- Oracle Common Function Library 參考文件OracleFunction
- ORACLE FUNCTION函式中DETERMINISTIC測試OracleFunction函式
- Oracle Date Function 講解和事例【Blog 搬家】OracleFunction