oracle 11g model子句的用法
oracle 11g model子句的用法
--1、語法
MODEL
[]
[]
[MAIN]
[PARTITION BY ()]
DIMENSION BY ()
MEASURES ()
[]
[RULES]
(, ,.., )
::=
::= RETURN {ALL|UPDATED} ROWS
::=
[IGNORE NAV | [KEEP NAV]
[UNIQUE DIMENSION | UNIQUE SINGLE REFERENCE]
::=
[UPDATE | UPSERT | UPSERT ALL]
[AUTOMATIC ORDER | SEQUENTIAL ORDER]
[ITERATE () [UNTIL ]]
::= REFERENCE ON ON ()
DIMENSION BY () MEASURES ()
[
[
[MAIN
[PARTITION BY (
DIMENSION BY (
MEASURES (
[
[RULES]
(
[IGNORE NAV | [KEEP NAV]
[UNIQUE DIMENSION | UNIQUE SINGLE REFERENCE]
[UPDATE | UPSERT | UPSERT ALL]
[AUTOMATIC ORDER | SEQUENTIAL ORDER]
[ITERATE (
DIMENSION BY (
還是直接看例子吧!
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select * from t order by years,months,product_name;
YEARS MONTHS PRODUCT_NA SALES
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
--用位置標記和符號標記訪問資料單元
SQL> select years,months,product_name,sales from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sales)
6 rules
7 (
8 sales[months=4,product_name='A']=sales[months=3,product_name='A']
9 );
SQL> select years,months,product_name,sales from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sales)
6 rules
7 (
8 sales[months=4,product_name='A']=sales[months=3,product_name='A']
9 );
YEARS MONTHS PRODUCT_NA SALES
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
已選擇6行。
--不用位置標記和符號標記訪問
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[4,'A']=sale[2,'A']
9 );
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[4,'A']=sale[2,'A']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
2008 4 A 2000
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 2000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
2008 4 A 2000
已選擇7行。
總結一:不用位置標記會將更新後的行也一起返回。
--使用return updated rows
SQL> select years,months,product_name,sales from t
2 model
3 return updated rows
4 partition by (years)
5 dimension by (months,product_name)
6 measures (sales sales)
7 rules
8 (
9 sales[months=4,product_name='A']=sales[months=3,product_name='A']
10 );
SQL> select years,months,product_name,sales from t
2 model
3 return updated rows
4 partition by (years)
5 dimension by (months,product_name)
6 measures (sales sales)
7 rules
8 (
9 sales[months=4,product_name='A']=sales[months=3,product_name='A']
10 );
未選定行
--不用位置標記和符號標記訪問
SQL> select years,months,product_name,sale from t
2 model
3 return updated rows
4 partition by (years)
5 dimension by (months,product_name)
6 measures (sales sale)
7 rules
8 (
9 sale[4,'A']=sale[2,'A']
10 );
SQL> select years,months,product_name,sale from t
2 model
3 return updated rows
4 partition by (years)
5 dimension by (months,product_name)
6 measures (sales sale)
7 rules
8 (
9 sale[4,'A']=sale[2,'A']
10 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 4 A 2000
---------- ---------- ---------- ----------
2008 4 A 2000
總結:使用return updated rows 以後,只返回有更新的行記錄。
--使用ANY和IS ANY
--ANY和位置標記一起使用,IS ANY和符號標記一起使用。
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[ANY,'A']=100
9 );
--ANY和位置標記一起使用,IS ANY和符號標記一起使用。
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[ANY,'A']=100
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 B 1500
2008 2 B 3000
2008 2 C 1000
2008 1 A 100
2008 2 A 100
2008 3 A 100
---------- ---------- ---------- ----------
2008 1 B 1500
2008 2 B 3000
2008 2 C 1000
2008 1 A 100
2008 2 A 100
2008 3 A 100
已選擇6行。
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[months IS ANY,'A']=100
9 );
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[months IS ANY,'A']=100
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 B 1500
2008 2 B 3000
2008 2 C 1000
2008 1 A 100
2008 2 A 100
2008 3 A 100
---------- ---------- ---------- ----------
2008 1 B 1500
2008 2 B 3000
2008 2 C 1000
2008 1 A 100
2008 2 A 100
2008 3 A 100
已選擇6行。
--總結:ANY和IS ANY簡化了rules中的規則;
--使用currentv()或者CV()函式
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[2,'A']=sale[cv(),'B']
9 );
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[2,'A']=sale[cv(),'B']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
已選擇6行。
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[2,'A']=sale[cv(months)-1,'B']
9 );
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[2,'A']=sale[cv(months)-1,'B']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 1500
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
---------- ---------- ---------- ----------
2008 1 A 1000
2008 1 B 1500
2008 2 A 1500
2008 2 B 3000
2008 2 C 1000
2008 3 A 3000
已選擇6行。
--使用FOR迴圈
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months from 1 to 3 increment 1,'A']=sale[cv(),'B']
9 );
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months from 1 to 3 increment 1,'A']=sale[cv(),'B']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A
已選擇6行。
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=sale[cv(),'B']
9 );
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=sale[cv(),'B']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A
已選擇6行。
--使用IS PRESENT
--如果資料單元指定的記錄在MODEL子句執行之前就存在,那麼IS PRESENT 返回TRUE
select years,months,product_name,sale from t
model
partition by (years)
dimension by (months,product_name)
measures (sales sale)
rules
(
sale[for months in (1,2,3),'A']=CASE WHEN sale[cv(),'B']
IS PRESENT THEN sale[cv(),'B'] ELSE 0 END
);
--如果資料單元指定的記錄在MODEL子句執行之前就存在,那麼IS PRESENT 返回TRUE
select years,months,product_name,sale from t
model
partition by (years)
dimension by (months,product_name)
measures (sales sale)
rules
(
sale[for months in (1,2,3),'A']=CASE WHEN sale[cv(),'B']
IS PRESENT THEN sale[cv(),'B'] ELSE 0 END
);
--使用PRESENTV()
--PRESENTV(CELL,EXPR1,EXPR2)如果cell引用的記錄存在,則返回EXPR1,否則返回EXPR2;
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=presentv(sale[cv(),'B'],sale[cv(),'B'],0)
9 );
--PRESENTV(CELL,EXPR1,EXPR2)如果cell引用的記錄存在,則返回EXPR1,否則返回EXPR2;
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=presentv(sale[cv(),'B'],sale[cv(),'B'],0)
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 0
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 0
已選擇6行。
--使用PRESENTNNV()
--PRESENTNNV(CELL,EXPR1,EXPR2)只有當cell單元引用的記錄存在且不為空時才返回EXPR1,否則返回EXPR2;
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=presentnnv(sale[cv(),'B'],sale[cv(),'B'],1)
9 );
--PRESENTNNV(CELL,EXPR1,EXPR2)只有當cell單元引用的記錄存在且不為空時才返回EXPR1,否則返回EXPR2;
SQL> select years,months,product_name,sale from t
2 model
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=presentnnv(sale[cv(),'B'],sale[cv(),'B'],1)
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 1
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 1
已選擇6行。
-
-使用IGNORE NAV和KEEP NAV
IGNORE NAV 的返回值如下:
空值或缺失數字值時返回0;
空值或缺失字串時返回空字串;
空值或缺失日期值時返回01-JAN-2000;
其他資料庫型別時返回空值;
KEEP NAV 對空值或缺失數字值返回空值,預設條件下是使用KEEP NAV
IGNORE NAV 的返回值如下:
空值或缺失數字值時返回0;
空值或缺失字串時返回空字串;
空值或缺失日期值時返回01-JAN-2000;
其他資料庫型別時返回空值;
KEEP NAV 對空值或缺失數字值返回空值,預設條件下是使用KEEP NAV
SQL> select years,months,product_name,sale from t
2 model ignore nav
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=sale[cv(),'B']
9 );
2 model ignore nav
3 partition by (years)
4 dimension by (months,product_name)
5 measures (sales sale)
6 rules
7 (
8 sale[for months in (1,2,3),'A']=sale[cv(),'B']
9 );
YEARS MONTHS PRODUCT_NA SALE
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 0
---------- ---------- ---------- ----------
2008 1 A 1500
2008 1 B 1500
2008 2 A 3000
2008 2 B 3000
2008 2 C 1000
2008 3 A 0
已選擇6行。
點評:MODEL子句是Oracle 11g新推出的特性,這種遞迴方式簡單易懂,比較好用。
參考自:oracle 11g sql開發指南及oracle 11gr2文件
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25954236/viewspace-1676605/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle行列轉換及pivot子句的用法Oracle
- (轉載)ORACLE MODEL子句學習筆記Oracle筆記
- [轉載] Oracle:start with...connect by子句的用法Oracle
- Oracle中start with...connect by prior子句用法.docOracle
- MySQL中BETWEEN子句的用法詳解MySql
- 詳解MySQL中WHERE子句的用法MySql
- oracle SQL with 子句OracleSQL
- ORACLE WITH CHECK OPTION子句詳解Oracle
- oracle rollup,cube子句的應用舉例;Oracle
- 達夢資料庫DM8中WITH子句的簡單用法資料庫
- Django model update的各種用法介紹Django
- 分析函式用法及視窗子句 range/rows差別函式
- SpringBoot @ModelAttribute 用法Spring Boot
- Django model select的各種用法詳解Django
- Oracle in子句過多的硬編碼引發的故障Oracle
- Oracle Model ClauseOracle
- Spring: @ModelAttribute註解用法Spring
- Vue3.4+版本中的 defineModel 宏的用法示例Vue
- Oracle Hints的用法Oracle
- oracle job的用法Oracle
- Oracle keep的用法Oracle
- Oracle Analyze的用法Oracle
- Oracle中with的用法Oracle
- Oracle Hint的用法Oracle
- Oracle的Cast的用法OracleAST
- opencv呼叫cv2.dnn_DetectionModel 用法OpenCVDNN
- Oracle SQL Model ClauseOracleSQL
- SQL中 where 子句和having子句中的區別SQL
- 【解決DML 語句包含不帶 INTO 子句的 OUTPUT 子句】
- ORACLE SQL開發where子句之case-whenOracleSQL
- oracle 建立檢視用 with check option 子句詳解Oracle
- TOP 子句
- ORACLE WITH AS 用法Oracle
- Oracle中where子句中條件的物理執行順序Oracle
- 對Oracle資料庫中Stroage子句的一些理解Oracle資料庫
- SQLite中的WHERE子句SQLite
- SQLite中的FROM子句SQLite
- oracle comment on的用法Oracle