Oracle Reporting 7 - Model Examples
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 between week 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
DEPARTMENT VARCHAR2 (30), commit;
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)
);
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 between week 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
DEPARTMENT VARCHAR2 (30), commit;
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)
);
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-1061262/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- examples for oracle ref cursorsOracle
- Oracle Model ClauseOracle
- Oracle SQL Model ClauseOracleSQL
- Error reporting for dbusError
- VirtualHost Examples
- simd examples
- crewAI-examplesAI
- gitglossary learning by examplesGit
- "bare repository" learning by examples
- airflow DAG/PIPELINE examples referenceAI
- Ten examples of git-archiveGitHive
- PostgreSQL DBA(62) - PG 12 More progress reportingSQL
- 7 Oracle 函式Oracle函式
- zero-shot-learning-definition-examples-comparison
- DevExpress v17.2新版亮點—.NET Reporting篇(二)devExpress
- DevExpress v17.2新版亮點—.NET Reporting篇(一)devExpress
- cannot find trajectory file at ./examples/trajectory.txt
- 【Basic Abstract Algebra】Exercises for Section 2.1 — Definitions and examples
- machine learning model(algorithm model) .vs. statistical modelMacGo
- 7 Oracle DataGuard 命令列參考Oracle命令列
- Microsoft Sql Server 2017 Reporting Services 不支援安裝在DC上ROSSQLServer
- basictracer-go原始碼閱讀——examples(完結)Go原始碼
- MIT6.S081 - Lecture1: Introduction and ExamplesMIT
- Oracle OCP(34):環境準備(Redhat 7 + Oracle 12c)OracleRedhat
- Oracle:Redhat 7 + Oracle RAC 11g 安裝 bug 總結OracleRedhat
- oracle O7_DICTIONARY_ACCESSIBILITY 引數Oracle
- Oracle VM VirtualBox安裝centos7OracleCentOS
- CentOS 7 - 安裝Oracle JDK8CentOSOracleJDK
- CentOS 7 安裝 Oracle12CCentOSOracle
- centos7 安裝ORACLE 11.2.0.4.0 RACCentOSOracle
- v2.5.4,changelog:add examples in json schema 例子沒找到JSON
- Oracle Linux 7使用syslog來管理Oracle ASM的審計檔案OracleLinuxASM
- Reactor ModelReact
- 解說pytorch中的model=model.to(device)PyTorchdev
- Dynamics CRM CRM Reporting Error: Error occurred while fetching the data extension的解決方法ErrorWhile
- 特斯拉Model 3雙電機版本將於7月投產:續航更久
- linux7 開機自啟動oracleLinuxOracle
- 達夢7異構(DM-Oracle) DBLINKOracle
- oracle linux 7 安裝圖形化介面OracleLinux