Oracle Reporting 7 - Model Examples
2013-12-16 13:02
274 查看
Looping in Model:
select product, country, year, week, inventory, sale, receipts from sales_fact where country in ('Australia') and product ='Xtend Memory' model return updated rows partition by (product, country) dimension by (year, week) measures ( 0 inventory , sale, receipts) rules automatic order( inventory [2002, week ] = nvl(inventory [cv(year), cv(week)-1 ] ,0) - sale[cv(year), cv(week) ] + + receipts [cv(year), cv(week) ], sale [2002, for week from 1 to 53 increment 1] = 0, receipts [ 2002,for week from 1 to 53 increment 1] =0 ) order by product, country,year, week;
Aggregations in Model:
select product, country, year, week, inventory, avg_inventory, max_sale from sales_fact where country in ('Australia') and product ='Xtend Memory' model return updated rows partition by (product,country) dimension by (year, week) measures ( 0 inventory ,0 avg_inventory , 0 max_sale, sale, receipts) rules automatic order( inventory [year, week ] = nvl(inventory [cv(year), cv(week)-1 ] ,0) - sale[cv(year), cv(week) ] + + receipts [cv(year), cv(week) ], avg_inventory [ year,ANY ] = avg (inventory) [ cv(year), week ], max_Sale [ year, ANY ] = max( sale) [ cv(year), week ] ) order by product, country,year, week;
Please note the difference betweenweek and cv(week) in the cell reference. In avg(inventory)[cv(year), week], "week" refers to all weeks of cv(year).
Using Iteration to pivot
select year, week,sale, sale_list from sales_fact where country in ('Australia') and product ='Xtend Memory' model return updated rows partition by (product, country) dimension by (year, week) measures ( cast(' ' as varchar2(50) ) sale_list, sale) rules iterate (5) ( sale_list [ year, week ] order by year, week = sale [cv(year), CV(week)-ITERATION_NUMBER +2 ] || case when iteration_number=0 then '' else ', ' end || sale_list [cv(year) ,cv(week)] ) order by year, week;
Iteration_number starts from 0 to 4. Rule for sale_list is an ordered rule. It accesses cells in the order prescribed by ORDER BY and applies the right side computation.
Presentv and nulls
select year, week,sale, sale_list from sales_fact where country in ('Australia') and product ='Xtend Memory' model return updated rows partition by (product, country) dimension by (year, week) measures ( cast(' ' as varchar2(120) ) sale_list, sale, 0 tmp) rules iterate (5) ( sale_list [ year, week ] order by year, week = presentv( sale [cv(year), CV(week)-iteration_number + 2 ], sale [cv(year), CV(week)-iteration_number + 2 ] || case when iteration_number=0 then '' else ', ' end || sale_list [cv(year) ,cv(week)], sale_list [cv(year) ,cv(week)]) ) order by year, week;
This query removes redendant commas.
Reference models - lookup table:
select year, week,sale, prod_list_price from sales_fact where country in ('Australia') and product ='Xtend Memory' model return updated rows REFERENCE ref_prod on (select prod_name, max(prod_list_price) prod_list_price from products group by prod_name) dimension by (prod_name) measures (prod_list_price) MAIN main_section partition by (product,country) dimension by (year, week) measures( sale, receipts, 0 prod_list_price ) rules ( prod_list_price[year,week] order by year, week = ref_prod.prod_list_price [ cv(product) ] ) order by year, week;
Oracle supports multiple reference models.
This example is based on the post: http://www.orafaq.com/node/69
CREATE TABLE FINANCIAL_STATEMENT ( REP_LINE_ID NUMBER, REP_LINE_ITEM VARCHAR2 (30), DEPARTMENT VARCHAR2 (30), ACTUAL NUMBER (10,2), BUDGET NUMBER (10,2), VARIANCE NUMBER (10,2) ); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 1, 'Net Sales', 'Retail', 5000, 5500, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 2, 'Gross Profit', 'Retail', 4000, 4750, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 3, 'Other Income', 'Retail', 900, 0, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 4, 'Total Net Income', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 5, 'Wage Costs', 'Retail', 3500, 3400, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 6, 'Rent Costs', 'Retail', 750, 750, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 7, 'Other Costs', 'Retail', 200, 0, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 8, 'Total Costs', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 9, 'Net Contribution', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 10, 'Gross Profit % of Net Sales', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 11, 'Wages % of Net Sales', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 12, 'Total Costs % of Net Sales', 'Retail', NULL, NULL, NULL); INSERT INTO FINANCIAL_STATEMENT ( REP_LINE_ID, REP_LINE_ITEM, DEPARTMENT, ACTUAL, BUDGET, VARIANCE ) VALUES ( 13, 'Net Contrib. % of Net Sales', 'Retail', NULL, NULL, NULL); commit; select rep_line_item,department, budget, actual actual, variance from financial_statement where department='Retail' model return all rows partition by (department) dimension by(rep_line_item) measures (actual actual, budget budget, variance variance) rules upsert( budget['Total Net Income'] = budget['Gross Profit'] + budget['Other Income'], actual['Total Net Income'] = actual['Gross Profit'] + actual['Other Income'], budget['Total Costs'] = budget['Wage Costs'] + budget['Rent Costs'] + budget['Other Costs'], actual['Total Costs'] = actual['Wage Costs'] + actual['Rent Costs'] + actual['Other Costs'], budget['Net Contribution'] = budget['Total Net Income'] - budget['Total Costs'], actual['Net Contribution'] = actual['Total Net Income'] - actual['Total Costs'], budget['Gross Profit % of Net Sales'] = Round(budget['Gross Profit']/budget['Net Sales'], 2), actual['Gross Profit % of Net Sales'] = Round(actual['Gross Profit']/actual['Net Sales'],2), budget['Wages % of Net Sales'] = Round(budget['Wage Costs']/budget['Net Sales'], 2), actual['Wages % of Net Sales'] = Round(actual['Wage Costs']/actual['Net Sales'],2), budget['Total Costs % of Net Sales'] = Round(budget['Total Costs']/budget['Net Sales'], 2), actual['Total Costs % of Net Sales'] = Round(actual['Total Costs']/actual['Net Sales'],2), budget['Net Contrib. % of Net Sales'] = Round(budget['Net Contribution']/budget['Net Sales'], 2), actual['Net Contrib. % of Net Sales'] = Round(actual['Net Contribution']/actual['Net Sales'],2), variance[ANY] = presentnnv(actual[cv(rep_line_item)],actual[cv(rep_line_item)],0) - presentnnv(budget[cv(rep_line_item)],budget[cv(rep_line_item)],0) );
相关文章推荐
- Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)
- Oracle Trigger Typical Examples
- Oracle Analytic SQL Practical Examples
- oracle服务器重启后无法进入系统,登录系统时提示model is unknow
- Oracle的model用法简介
- 将 Entity Framework、LINQ 和 Model-First 用于 Oracle 数据库
- 笔记:Oracle SQL 高级查询简介 (3) MODEL子句,PIVOT与UNPIVOT子句
- oracle编程入门笔记2015-01-28--model子句原理
- Oracle的model语句入门
- Oracle分析函数三、基于model函数建设口径统一的统计指标库
- Oracle 10g Transparent Data Encryption examples
- Oracle 11g学习笔记--model子句
- oracle 新功能model子句的简单使用
- 通过案例学调优之--Oracle Time Model(时间模型)
- Oracle的 MODEL 查询
- Oracle_PO_ER图/Oralce_PR_ER_Model/Standard_PO_Flow
- Oracle CRM On Demand Reporting
- ORACLE MODEL子句学习笔记
- SharePoint 2010 with Reporting Services 2008 dealing with oracle data
- 《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.4Returning Updated Rows