1
2
22
2
3
4
centralised
backed up
secure
5
job done ?
6
6 months later
7
8
9
10
11
12
McDonald's Conjecture
on Large Corporations:
14
"you cannot kill Excel"
15
16
custom queries
adhoc reports
17
18
19
20
you cannot kill Excel
21
"why?"
22
23
24
formulas
inter-row calculations
familiar excel expressions
25
compare traditional
26
design,
document,
signoff,
PLSQL,
develop,
test,
implement
27
28
you
cannot
kill
Excel
29
30
formulas
inter-row calculations
familiar excel expressions
...all in SQL
31
"end user metaphor"
32
33
model clause replicates this
34
my first MODEL query
35
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win';
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
36
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win';
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
37
excel = MODEL
38
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ()
11 /
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
39
paid by line of code ...
40
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ()
11 /
41
dimensions
42
dimension
43
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ()
11 /
44
measures
45
dimension
measures
46
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ()
11 /
47
rules
defined by dimension,
impact measures
48
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ( ver[2009]='Windows7',
11 bugs[2009]=150 );
49
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ( ver[2009]='Windows7',
11 bugs[2009]=150 );
50
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ( ver[2009]='Windows7',
11 bugs[2009]=150 );
51
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ( ver[2009]='Windows7',
11 bugs[2009]=150
12 )
13 /
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
2009 Windows7 150
52
relational data
dimensioned array
relational data
53
you'll still need "Excel"
Discoverer
APEX
Crystal Reports
54
a closer look
55
too very flexible
56
57
create measures
58
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules sequential order
11 ( est_bugs[1995]=100 )
12 order by year;
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 100
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
59
60
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules sequential order
11 (
12 est_bugs[1995]=100 ,
13 est_bugs[1998]=100 ,
14 est_bugs[2000]=100 ,
15 est_bugs[2002]=100
16 ...
61
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules sequential order
11 (
12 est_bugs[ANY]=100
13 );
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 100
1998 Windows98 130 100
2000 WindowsME 170 100
2002 WindowsXP 140 100
62
63
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules sequential order
11 ( est_bugs[1995]=bugs[1995]*1.1 )
12 order by year;
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 110
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
64
65
back reference
66
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 ( est_bugs[ANY]=bugs[CV()]*1.1
12 )
13 /
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 110
1998 Windows98 130 143
2000 WindowsME 170 187
2002 WindowsXP 140 154
67
multiple dimensions
68
69
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (department, year)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 ( est_bugs['HR', ANY]=bugs['HR', CV()]*1.1
12 ( est_bugs['Sales', ANY]=bugs['Sales', CV()]*1.2
13 ( est_bugs['Payroll', ANY]=bugs['Payroll', CV()]*1.5
14 )
15 /
not just numeric
70
71
dimensions define a cell
72
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model
7 dimension by (year)
8 measures (ver,bugs, 0 est_bugs)
9 rules
10 (
11 );
YR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
1996 RedHat 50 0
2006 Oracle 80 0
2001 SUSE 65 0
2003 CentOS 90 0
73
SQL> insert into versions
2 values ('Linux',2001,'Fedora',70);
1 row created.
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model
7 dimension by (year as yr)
8 measures (ver,bugs, 0 est_bugs)
9 rules
10 (
11 );
from versions
*
ERROR at line 5:
ORA-32638: Non unique addressing in MODEL dimensions
74
75
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model UNIQUE SINGLE REFERENCE
7 dimension by (year as yr)
8 measures (ver,bugs, 0 est_bugs)
9 rules ( );
YR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
1996 RedHat 50 0
2006 Oracle 80 0
2001 SUSE 65 0
2003 CentOS 90 0
2001 Fedora 70 0
76
unique single reference
rules cannot cross dimensions
77
dimensions can be null
78
SQL> select yr,ver,bugs,est_bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year as yr)
6 measures (ver,bugs, 0 est_bugs)
7 rules
8 (
9 est_bugs[null]=140
10 );
YR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
(null) 140
79
idiosyncracies # 1
80
SQL> select year as yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model
7 dimension by (yr)
8 measures (ver,bugs, 0 est_bugs)
9 rules
10 (
11 );
dimension by (yr)
*
ERROR at line 7:
ORA-00904: "YR": invalid identifier
81
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model
7 dimension by (year as yr)
8 measures (ver,bugs, 0 est_bugs)
9 rules
10 (
11 );
YR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
1996 RedHat 50 0
2006 Oracle 80 0
2001 SUSE 65 0
2003 CentOS 90 0
82
more on measures
83
any standard expression
84
SQL> select ...
7 model
8 dimension by (year)
9 measures (ver,
10 bugs,
11 bugs*0.5 half_bugs,
12 avg(bugs) over (
13 order by year rows
14 between 1 preceding
15 and 1 following ) est_bugs)
16 rules
17 (
18 );
YEAR VER BUGS HALF_BUGS EST_BUGS
---------- ------------ ---------- ---------- ----------
1995 Windows95 100 50 115
1998 Windows98 130 65 133.333333
2000 WindowsME 170 85 146.666667
2002 WindowsXP 140 70 155
85
aggregates
86
SQL> select platform, tot_bugs
2 from versions
3 group by platform
4 model
5 dimension by (platform)
6 measures (sum(bugs) as tot_bugs)
7 rules
8 (
9 );
PLATFORM TOT_BUGS
---------- ----------
Linux 285
Win 540
87
idiosyncracy # 2
88
SQL> select platform, sum(bugs) as tot_bugs
2 from versions
3 group by platform
4 model
5 dimension by (platform)
6 measures (tot_bugs)
7 rules
8 (
9 );
measures (tot_bugs)
*
ERROR at line 6:
ORA-00904: "TOT_BUGS": invalid identifier
89
idiosyncracies # 3
90
SQL> select platform, year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs)
7 rules
8 ( ver[2009]='Windows7',
9 bugs[2009]=150
10 );
select platform, year, ver, bugs
*
ERROR at line 1:
ORA-32614: illegal MODEL SELECT expression
91
SQL> select platform, year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,platform)
7 rules
8 ( ver[2009]='Windows7',
9 bugs[2009]=150
10 );
PLATFORM YEAR VER BUGS
---------- ---------- --------------- ----------
Win 1995 Windows95 100
Win 1998 Windows98 130
Win 2000 WindowsME 170
Win 2002 WindowsXP 140
2009 Windows7 150
92
idiosyncracies # 4
93
SQL> select year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,year)
7 rules
8 ( ver[2009]='Windows7',
9 bugs[2009]=150,
10 year[2009]=2010
11 );
select year, ver, bugs
*
ERROR at line 1:
ORA-00957: duplicate column name
94
SQL> select year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,year next_year)
7 rules
8 ( ver[2009]='Windows7',
9 bugs[2009]=150,
10 next_year[2009]=2010
11 );
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
2009 Windows7 150
95
SQL> select year, ver, bugs, next_year
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,year next_year)
7 rules
8 ( ver[2009]='Windows7',
9 bugs[2009]=150,
10 next_year[2009]=2010
11 );
YEAR VER BUGS NEXT_YEAR
---------- --------------- ---------- ----------
1995 Windows95 100 1995
1998 Windows98 130 1998
2000 WindowsME 170 2000
2002 WindowsXP 140 2002
2009 Windows7 150 2010
96
idiosyncracies # 5
97
SQL> select year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,null est_sales)
7 rules
8 (
9 est_sales[ANY]=0
10 );
measures (ver,bugs,null est_sales)
*
ERROR at line 6:
ORA-01723: zero-length columns are not allowed
98
SQL> select year, ver, bugs
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,to_number(null) est_sales)
7 rules
8 (
9 est_sales[ANY]=0
10 );
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
99
SQL> select year, ver, bugs, sales_rep
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,
7 cast(null as varchar2(20)) sales_rep)
8 rules
9 (
10 sales_rep[2000]='Mike'
11 );
YEAR VER BUGS SALES_REP
---------- --------------- ---------- --------------------
1995 Windows95 100
1998 Windows98 130
2002 WindowsXP 140
2000 WindowsME 170 Mike
100
SQL> select year, ver, bugs, sales_rep
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,'Unknown' sales_rep)
7 rules
8 (
9 sales_rep[2000]='Mike Jones'
10 );
ERROR:
ORA-25137: Data value out of range
101
SQL> select year, ver, bugs, sales_rep
2 from versions
3 where platform = 'Win'
4 model
5 dimension by (year)
6 measures (ver,bugs,
7 cast('Unknown' as varchar2(20)) sales_rep)
8 rules
9 (
10 sales_rep[2000]='Mike Jones'
11 );
YEAR VER BUGS SALES_REP
---------- --------------- ---------- --------------------
1995 Windows95 100 Unknown
1998 Windows98 130 Unknown
2002 WindowsXP 140 Unknown
2000 WindowsME 170 Mike Jones
102
partitions
103
104
SQL> select platform
...
6 from versions
7 model
8 partition by ( platform )
9 dimension by (year)
10 measures (ver,bugs, 0 est_bugs)
11 rules
12 ( est_bugs[1995]=bugs[1995],
13 est_bugs[year > 1995]=bugs[CV()]*1.1
14 );
PLATFORM YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
Linux 1996 RedHat 50 55
Linux 2006 Oracle 80 88
Linux 2001 SUSE 65 71.5
Linux 2003 CentOS 90 99
Linux 1995
Win 1995 Windows95 100 100
Win 1998 Windows98 130 143
Win 2000 WindowsME 170 187
Win 2002 WindowsXP 140 154
105
rules applied per partition
106
rules
107
standard expressions
108
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[1998]=100,
13 bugs[2000]= :bindvar,
14 est_bugs[2009]= dbms_random.value(80,100)
15 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
2002 WindowsXP 140 0
1998 Windows98 130 100
2000 WindowsME <:bv> 0
2009 90.1501647
109
"no" subqueries
110
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[2009]=
13 ( select max(bugs) from versions )
14 );
select yr
*
ERROR at line 1:
ORA-32620: illegal subquery within MODEL rules
111
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs,
10 ( select max(bugs) from versions ) x)
11 rules
12 (
13 est_bugs[2009]= x[2000]
14 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2002 WindowsXP 140 0
2000 WindowsME 170 0
2009 170
112
CV( )
113
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 ( est_bugs[ANY]=bugs[CV()]*1.1
12 )
13 /
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100 110
1998 Windows98 130 143
2000 WindowsME 170 187
2002 WindowsXP 140 154
114
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs, 0 zero_bug)
10 rules
11 ( zero_bug[ANY]=CV()+10
12 );
( zero_bug[ANY]=CV()+10
*
ERROR at line 11:
ORA-32611: incorrect use of MODEL CV operator
115
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 ,zero_bug
6 from versions
7 where platform = 'Win'
8 model
9 dimension by (year)
10 measures (ver,bugs, 0 est_bugs, 0 zero_bug)
11 rules
12 ( zero_bug[ANY]=CV(YEAR)+10
13 );
YEAR VER BUGS EST_BUGS ZERO_BUG
---------- ------------ ---------- ---------- ----------
1995 Windows95 100 0 2005
1998 Windows98 130 0 2008
2000 WindowsME 170 0 2010
2002 WindowsXP 140 0 2012
116
cross-row rules
117
SQL> select fib
2 from ( select rownum r from dual
3 connect by level <= 10 )
4 model
5 dimension by (r)
6 measures (0 fib)
7 rules
8 ( fib[1]=1,
9 fib[2]=1,
10 fib[r>2]=fib[CV()-1]+fib[CV()-2] );
FIB
----------
1
1
2
3
5
8
13
21
34
55
118
key point
119
symbolic versus positional
120
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[2009]=100,
13 ver[2009]='Windows7'
14 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
2009 Windows7 100
update or create a
dimension value of '2009'
121
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[yr=2009]=100,
13 ver[yr=2009]='Windows7'
14 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
update if exists a
dimension value of '2009'
122
FOR
123
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[yr IN (2000,2009)]=100
13 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2002 WindowsXP 140 0
2000 WindowsME 170 100
124
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[FOR yr IN (2000,2009)]=100
13 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2002 WindowsXP 140 0
2000 WindowsME 170 100
2009 100
125
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[FOR yr FROM 2002 TO 2005 INCREMENT 1]=100
13 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 100
2003 100
2004 100
2005 100
126
SQL> select ...
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[FOR yr IN
13 ( select year
14 from versions
15 where platform = 'Linux') ]=100
16 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2000 WindowsME 170 0
2002 WindowsXP 140 0
1996 100
2006 100
2001 100
2003 100
127
update vs upsert
128
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[FOR yr IN (2000,2009)]=100,
13 ver[2009]='Windows7' );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2002 WindowsXP 140 0
2000 WindowsME 170 100
2009 Windows7 100
UPSERT
129
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules UPDATE
11 (
12 est_bugs[FOR yr IN (2000,2009)]=100,
13 ver[2009]='Windows7' );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 0
2002 WindowsXP 140 0
2000 WindowsME 170 100
2009 Windows7 100
2009 gone
130
idiosyncracies # 2
the "sert" in upsert
131
SQL> select *
2 from versions
3 where platform = 'z/OS';
no rows selected
SQL> select *
2 from versions
3 where platform = 'z/OS'
4 model
5 dimension by (year as yr)
6 measures (ver, 0 est_bugs)
7 rules
8 ( est_bugs[FOR yr IN (2000,2002,2009)]=100,
9 ver[ANY]='IBM' );
YR VER EST_BUGS
---------- --------------- ----------
2000 IBM 100
2002 IBM 100
2009 IBM 100
132
note: simplification
upsert
upsert all
update
133
nested references
134
SQL> select yr
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs, year as yr2)
10 rules
11 (
12 yr2[ANY]=yr2[CV()]-2,
13 est_bugs[yr2[2000]]=100
14 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 0
1998 Windows98 130 100
2000 WindowsME 170 0
2002 WindowsXP 140 0
yr2[2000]=2000-2=1998
est_bugs[1998]=100
135
most typical SQL applies
136
SQL> select ...
7 model
8 dimension by (year as yr)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs[yr is null]=100,
13 est_bugs[yr is not null]=110,
14 est_bugs[yr > 0]=120,
15 est_bugs[yr between 2000 and 2010]=130,
16 est_bugs[null]=140
17 );
YR VER BUGS EST_BUGS
---------- ------------ ---------- ----------
1995 Windows95 100 120
1998 Windows98 130 120
2000 WindowsME 170 130
2002 WindowsXP 140 130
(null) 140
137
idiosyncracies # 3
138
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 model
7 dimension by (platform,year)
8 measures (ver,bugs, 0 est_bugs)
9 rules
10 (
11 est_bugs[2009]=100
12 );
est_bugs[2009]=100
*
ERROR at line 11:
ORA-00947: not enough values
139
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 (
12 est_bugs['Win',2009]=100
13 );
est_bugs['Win',2009]=100
*
ERROR at line 12:
ORA-00947: not enough values
140
rule aggregates
141
SQL> select year
2 ,ver
3 ,bugs
4 ,cumtot
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 cumtot)
10 rules
11 ( cumtot[ANY]=SUM(bugs)[year <= CV(year)]
12 );
YEAR VER BUGS CUMTOT
---------- ------------ ---------- ----------
1995 Windows95 100 100
1998 Windows98 130 230
2000 WindowsME 170 400
2002 WindowsXP 140 540
142
SQL> select year
2 ,ver
3 ,bugs
4 ,wt
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 wt)
10 rules
11 ( wt[ANY]=bugs[CV()]*
12 row_number() over ( order by year )
13 );
YEAR VER BUGS WT
---------- ------------ ---------- ----------
1995 Windows95 100 100
1998 Windows98 130 260
2000 WindowsME 170 510
2002 WindowsXP 140 560
143
rule conflict
144
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules automatic order
10 ( bugs[2000]=150,
11 bugs[ANY]=bugs[CV()]*1.1
12 )
13 /
bugs[ANY]=bugs[CV()]*1.1
*
ERROR at line 11:
ORA-32630: multiple assignment in automatic
order MODEL
145
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules sequential order
10 ( bugs[2000]=150,
11 bugs[ANY]=bugs[CV()]*1.1
12 )
13 /
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 110
1998 Windows98 143
2000 WindowsME 165
2002 WindowsXP 154
146
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model
7 dimension by (year)
8 measures (ver,bugs)
9 rules sequential order
10 ( bugs[ANY]=bugs[CV()]*1.1,
11 bugs[2000]=150
12 )
13 /
YEAR VER BUGS
---------- --------------- ----------
1995 Windows95 110
1998 Windows98 143
2000 WindowsME 150
2002 WindowsXP 154
110
143
165
154
147
rule affected rows only
148
SQL> select year
2 ,ver
3 ,bugs
4 from versions
5 where platform = 'Win'
6 model return updated rows
7 dimension by (year)
8 measures (ver,bugs)
9 rules
10 ( ver[2009]='Windows7',
11 bugs[2009]=150
12 )
13 /
YEAR VER BUGS
---------- --------------- ----------
2009 Windows7 150
149
inter-row measures
150
151
SQL> select year
2 ,ver
3 ,bugs
4 ,est_bugs
5 from versions
6 where platform = 'Win'
7 model
8 dimension by (year)
9 measures (ver,bugs, 0 est_bugs)
10 rules
11 ( est_bugs[ANY]=bugs[CV()-1]*1.1
12 )
13 /
YEAR VER BUGS EST_BUGS
---------- --------------- ---------- ----------
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140
est_bugs[1995]=bugs[1994]
est_bugs[1998]=bugs[1997]
est_bugs[2000]=bugs[1999]
est_bugs[2002]=bugs[2001]
152
non-existent dimensions = null
153
SQL> select rank() over ( order by year) as year_rank
2 ,year
3 ,ver
4 ,bugs
5 from versions
6 where platform = 'Win'
7 /
YEAR_RANK YEAR VER BUGS
---------- ---------- --------------- ----------
1 1995 Windows95 100
2 1998 Windows98 130
3 2000 WindowsME 170
4 2002 WindowsXP 140
154
SQL> select year_rank,year
2 ,ver,bugs,est_bugs
3 from versions
4 where platform = 'Win'
5 model
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,0 est_bugs)
9 rules
10 ( est_bugs[1]=bugs[1],
11 est_bugs[year_rank > 1]=bugs[CV()-1]*1.1
12 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100 100
2 1998 Windows98 130 110
3 2000 WindowsME 170 143
4 2002 WindowsXP 140 187
155
non-existent dimensions
in excel
156
157
158
SQL> select year_rank,year
2 ,ver,bugs,est_bugs
3 from versions
4 where platform = 'Win'
5 model
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,0 est_bugs)
9 rules
10 ( est_bugs[1]=bugs[1],
11 est_bugs[year_rank > 1]=bugs[CV()-1]*1.1
12 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100 100
2 1998 Windows98 130 110
3 2000 WindowsME 170 143
4 2002 WindowsXP 140 187
159
SQL> select year_rank,year
2 ,ver,bugs,est_bugs
3 from versions
4 where platform = 'Win'
5 model
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,0 est_bugs)
9 rules
10 ( est_bugs[ANY]=bugs[CV()-1]*1.1
11 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100
2 1998 Windows98 130 110
3 2000 WindowsME 170 143
4 2002 WindowsXP 140 187
160
but can be overridden
161
SQL> select year_rank,year
2 ,ver,bugs,est_bugs
3 from versions
4 where platform = 'Win'
5 model IGNORE NAV
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,0 est_bugs)
9 rules
10 ( est_bugs[ANY]=bugs[CV()-1]*1.1
11 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100 0
2 1998 Windows98 130 110
3 2000 WindowsME 170 143
4 2002 WindowsXP 140 187
162
SQL> select *
2 from dual
3 where 1=0
4 model ignore nav
5 dimension by (0 tag)
6 measures (to_date(null) d,
7 to_number(null) n,
8 cast(null as varchar2(10)) v)
9 rules
10 ( d[0] = d[1],
11 n[0] = n[1],
12 v[0] = v[1]
13 );
TAG D N V
---------- ------------------- ---------- ------
0 01/01/2000 00:00:00 0
dimension 1 does not exist
beware documentation !
163
useful for cascading measures
164
SQL> select year_rank,year
2 ,ver,bugs,cum_bugs
3 from versions
4 where platform = 'Win'
5 model
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,to_number(null) cum_bugs)
9 rules
10 ( cum_bugs[ANY]=cum_bugs[CV()-1]+bugs[CV()-1]
11 );
YEAR_RANK YEAR VER BUGS CUM_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100
2 1998 Windows98 130
3 2000 WindowsME 170
4 2002 WindowsXP 140
165
SQL> select year_rank,year
2 ,ver,bugs,cum_bugs
3 from versions
4 where platform = 'Win'
5 model IGNORE NAV
6 dimension by (
7 rank() over ( order by year) as year_rank)
8 measures (ver,bugs,year,to_number(null) cum_bugs)
9 rules
10 ( cum_bugs[ANY]=cum_bugs[CV()-1]+bugs[CV()-1]
11 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- --------------- ---------- ----------
1 1995 Windows95 100 0
2 1998 Windows98 130 100
3 2000 WindowsME 170 230
4 2002 WindowsXP 140 400
166
rule ordering
167
SQL> select ...
6 from versions
7 where platform = 'Win'
8 model
9 dimension by (rank() over ( order by year) as year_rank)
10 measures (ver,bugs,year,bugs est_bugs)
11 rules
12 (
13 est_bugs[ANY]=est_bugs[CV()-1]*1.1
14 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- ------------ ---------- ----------
1 1995 Windows95 100
2 1998 Windows98 130
3 2000 WindowsME 170
4 2002 WindowsXP 140
168
SQL> select ...
6 from versions
7 where platform = 'Win'
8 model
9 dimension by (rank() over ( order by year) as year_rank)
10 measures (ver,bugs,year,bugs est_bugs)
11 rules
12 (
13 est_bugs[ANY] order by year_rank desc =
14 est_bugs[CV()-1]*1.1
15 );
YEAR_RANK YEAR VER BUGS EST_BUGS
---------- ---------- ------------ ---------- ----------
1 1995 Windows95 100
2 1998 Windows98 130 110
3 2000 WindowsME 170 143
4 2002 WindowsXP 140 187
169
lots of null "variations"
column is null
dimension does not exist
ignore nav / keep nav
170
SQL> select year, ver, bugs, patches
2 from versions
3 where platform = 'Win';
YEAR VER BUGS PATCHES
---------- --------------- ---------- ----------
1994 Windows3.1.1 140
1995 Windows95 100
1998 Windows98 130
2000 WindowsME 170
2002 WindowsXP 140 3
2003 Windows2003 140 1
FCAST
----------
(null)
(null)
(null)
(null)
(null)
110
"forecast = bugs(year-1) – 10*patches(year-1)"
171
SQL> select year, ver, patches, fcast, pv, pnv
2 from versions
3 where platform = 'Win'
4 model
5 dimension by ( year )
6 measures (ver, bugs, 0 fcast, patches, 'X' pv, 'X' pnv)
7 rules
8 (
9 fcast[ANY]=bugs[CV()]-10*patches[CV()-1],
10 pv[ANY]=PRESENTV( patches[CV()-1], 'Y','N'),
11 pnv[ANY]=PRESENTNNV( patches[CV()-1], 'Y','N')
12 );
YEAR VER PATCHES FCAST PV PNV
---------- --------------- ---------- ---------- ---- ----
1994 Windows3.1.1 N N
1995 Windows95 Y N
1998 Windows98 N N
2000 WindowsME N N
2002 WindowsXP 3 N N
2003 Windows2003 1 110 Y Y
172
"duh...I know about NVL"
resultset time
rule execution time
173
iteration
174
"if the number of bugs drops by
4% each year, what is the 5 year
forecast for each product"
Windows XP 140
Windows XP 134
Windows XP 129
Windows XP 123
Windows XP 118
Windows XP 113
175
SQL> select year, ver, bugs, bugs5yr
2 from versions
3 where platform = 'Win'
4 model
5 dimension by ( year )
6 measures (ver, bugs, bugs bugs5yr)
7 rules ITERATE(5)
8 (
9 bugs5yr[ANY]=bugs5yr[CV()] * 0.96
10 );
YEAR VER BUGS BUGS5YR
---------- --------------- ---------- -------
1995 Windows95 100 81.54
1998 Windows98 130 106.00
2000 WindowsME 170 138.61
2002 WindowsXP 140 114.15
176
5 iterations per partition
177
iteration_number
178
SQL> select num
2 from dual
3 model
4 dimension by ( 0 x)
5 measures ( 0 num)
6 rules iterate (20)
7 (
8 num[ITERATION_NUMBER]=ITERATION_NUMBER
9 );
NUM
----------
0
1
2
3
4
...
17
18
19
20 rows selected.
starts from zero
179
iterate UNTIL
180
loan payments
181
SQL> select *
2 from loans;
DESCR YEAR BAL RATE PMT
---------- ---------- ---------- ---------- ----------
Car 2000 25000 1.13 350
House 2010 300000 1.04 2200
182
SQL> select descr, bal final_bal, yrs
2 from loans
3 model
4 partition by ( year)
5 dimension by (0 idx)
6 measures (descr, bal, rate, pmt, 0 yrs)
7 rules iterate (50) until bal[0]*rate[0]-12*pmt[0]<0
8 (
9 bal[0]=bal[0]*rate[0]-12*pmt[0]
10 ,yrs[0]=iteration_number
11 );
DESCR FINAL_BAL YRS
---------- ----------- ----------
House 11660.34 14
Car 632.33 11
183
PREVIOUS
184
"how many years will it take for my
house to appreciate in value more
than my annual mortgage payments"
185
SQL> select year, bal, apprec, yrs
2 from loans
3 where descr = 'House'
4 model
5 partition by ( year )
6 dimension by ( 0 idx)
7 measures ( year y, bal, 12*pmt mort, apprec, 0 yrs)
8 rules iterate (20)
9 until bal[0]- previous( bal[0] ) > mort[0]
10 (
11 bal[0]=bal[0]*(1+apprec[0]/100),
12 yrs[0]=iteration_number+1
13 );
YEAR BAL APPREC YRS
---------- ---------- --------- ----------
2010 437742.689 6.5 6
186
reference models
187
SQL> select * from VERSIONS;
PLATFORM YEAR VER BUGS
---------- ---------- --------------- ----------
Win 1995 Windows95 100
Win 1998 Windows98 130
Win 2000 WindowsME 170
Win 2002 WindowsXP 140
Linux 1996 RedHat 50
Linux 2006 Oracle 80
Linux 2001 SUSE 65
Linux 2003 CentOS 90
SQL> select * from PLATFORM_TYPE;
PLATFORM OWNERSHIP
-------------------- --------------------
Win Private
Linux Open Source
188
SQL> select year, ver, bugs, est_bugs, own_ship
2 from versions
3 model
4 reference pt on
5 ( select platform p, ownership own
6 from platform_type )
7 dimension by (p)
8 measures (own)
9 dimension by (year)
10 measures (ver,bugs, 0 est_bugs,
11 platform, cast(null as varchar2(20)) own_ship)
12 rules
13 ( est_bugs[1995]=bugs[1995],
14 est_bugs[year > 1995]=bugs[cv()]*1.1,
15 own_ship[any]=pt.own[platform[cv()]]
16 );
YEAR VER BUGS EST_BUGS OWN_SHIP
---------- --------------- ---------- ---------- -----------
1995 Windows95 100 100 Private
1998 Windows98 130 143 Private
2000 WindowsME 170 187 Private
2002 WindowsXP 140 154 Private
1996 RedHat 50 55 Open Source
2006 Oracle 80 88 Open Source
2001 SUSE 65 71.5 Open Source
2003 CentOS 90 99 Open Source189
why not just a join ?
190
“imagination is more
important than knowledge”
- Albert Einstein
191
bit strings
192
SQL> create table DEC as
2 select rownum r
3 from dual connect by level < 1000;
Table created.
SQL> select * from DEC;
R
----------
1
2
3
4
5
6
7
8
9
10
193
SQL> select r, ltrim(str,'0')
2 from DEC
3 model
4 dimension by (r)
5 measures (
6 cast('' as varchar2(64)) str,
7 r as runtot,
8 power(2,32) as chk)
9 rules iterate(33)
10 ( str[ANY] =
11 str[CV()] ||
12 case
13 when bitand(runtot[CV()],chk[CV()]) > 0
14 then '1' else '0'
15 end,
16 runtot[ANY] =
17 runtot[CV()] -
18 bitand(runtot[CV()],chk[CV()]),
19 chk[ANY] = chk[CV()] / 2
20 );
194
R BITSTR
-------- -----------------
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000
17 10001
18 10010
195
or ...
196
select r, ltrim(str,'0')
from DEC
model
partition by (r)
dimension by (1 blah)
measures (
cast('' as varchar2(64)) str,
r as runtot,
power(2,32) as chk)
rules iterate(33)
( str[1] =
str[1] ||
case
when bitand(runtot[1],chk[1]) > 0
then '1' else '0'
end,
runtot[1] =
runtot[1] -
bitand(runtot[1],chk[1]),
chk[1] = chk[1] / 2
);
197
avoid self join
198
SQL> select e1.ename, e2.ename manager, e1.sal, e1.comm
2 from emp e1, emp e2
3 where e1.mgr= e2.empno;
ENAME MANAGER SAL COMM
---------- ---------- ---------- ----------
JONES KING 2975
CLARK KING 2450
BLAKE KING 2850
WARD BLAKE 1250 500
JAMES BLAKE 950
TURNER BLAKE 1500 0
ALLEN BLAKE 1600 300
MARTIN BLAKE 1250 1400
199
SQL> select ename, manager, sal, comm
2 from emp
3 model
4 dimension by (empno)
5 measures (ename, mgr, sal, comm,
6 cast (null as varchar2(10)) as manager)
7 rules (
8 manager[any] = ename[mgr[cv()]]
9 );
ENAME MANAGER SAL COMM
---------- ---------- ---------- ----------
KING ALLEN 5000
BLAKE KING 2850
CLARK KING 2450
JONES KING 2975
MARTIN BLAKE 1250 1400
ALLEN BLAKE 1600 300
...
200
string concatenation
pre 11.2
201
SQL> select deptno, ltrim(str,',') str
2 from emp
3 model return updated rows
4 partition by (deptno)
5 dimension by (
6 rank() over ( partition by deptno order by empno) x )
7 measures ( ename, cast('' as varchar2(4000)) str)
8 rules iterate (20)
9 until presentv(ename[iteration_number+1],1,2)=2
10 (
11 str[1] = str[1] || ','|| ename[iteration_number+1]
12 );
DEPTNO STR
---------- -------------------------------------------------
20 SMITH,JONES,SCOTT,ADAMS,FORD,
10 CLARK,KING,MILLER,
30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES,
202
obvious targets
203
SQL> select
2 col1,
3 col2,
4 my_function(...)
5 from EMP;
204
wrapup
205
why model ?
206
(largish) learning curve
207
SELECT c, p, m, pp, ip FROM MORTGAGE
MODEL
REFERENCE R ON
(SELECT customer, fact, amt FROM mortgage_facts
MODEL DIMENSION BY (customer, fact) MEASURES (amount amt)
RULES
(amt[any, 'PaymentAmt']= (amt[CV(),'Loan']*
Power(1+ (amt[CV(),'Annual_Interest']/100/12),
amt[CV(),'Payments']) *
(amt[CV(),'Annual_Interest']/100/12)) /
(Power(1+(amt[CV(),'Annual_Interest']/100/12),
amt[CV(),'Payments']) - 1)))
DIMENSION BY (customer cust, fact) measures (amt)
MAIN amortization
PARTITION BY (customer c)
DIMENSION BY (0 p)
MEASURES (principalp pp, interestp ip, mort_balance m, customer mc)
RULES
ITERATE(1000) UNTIL (ITERATION_NUMBER+1 =r.amt[mc[0],'Payments'])
(ip[ITERATION_NUMBER+1] = m[CV()-1] *
r.amt[mc[0], 'Annual_Interest']/1200,
pp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'] - ip[CV()],
m[ITERATION_NUMBER+1] = m[CV()-1] - pp[CV()] )
208
209
McDonald's Conjecture
210
you cannot kill Excel
211
let them !
212
all the work
formulas
layout rules
lookups
213
model equivalent
214
sharable
scalability
secure
flexible
215
accessible
216
ora-3113
217
Connor McDonald, www.oracledba.co.uk

The SQL Model Clause

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win'; YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 36
  • 37.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win'; YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 37
  • 38.
  • 39.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 () 11 / YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 39
  • 40.
    paid by lineof code ... 40
  • 41.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 () 11 / 41
  • 42.
  • 43.
  • 44.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 () 11 / 44
  • 45.
  • 46.
  • 47.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 () 11 / 47
  • 48.
  • 49.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 ( ver[2009]='Windows7', 11 bugs[2009]=150 ); 49
  • 50.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 ( ver[2009]='Windows7', 11 bugs[2009]=150 ); 50
  • 51.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 ( ver[2009]='Windows7', 11 bugs[2009]=150 ); 51
  • 52.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 ( ver[2009]='Windows7', 11 bugs[2009]=150 12 ) 13 / YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 2009 Windows7 150 52
  • 53.
  • 54.
    you'll still need"Excel" Discoverer APEX Crystal Reports 54
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules sequential order 11 ( est_bugs[1995]=100 ) 12 order by year; YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 100 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 59
  • 60.
  • 61.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules sequential order 11 ( 12 est_bugs[1995]=100 , 13 est_bugs[1998]=100 , 14 est_bugs[2000]=100 , 15 est_bugs[2002]=100 16 ... 61
  • 62.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules sequential order 11 ( 12 est_bugs[ANY]=100 13 ); YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 100 1998 Windows98 130 100 2000 WindowsME 170 100 2002 WindowsXP 140 100 62
  • 63.
  • 64.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules sequential order 11 ( est_bugs[1995]=bugs[1995]*1.1 ) 12 order by year; YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 110 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 64
  • 65.
  • 66.
  • 67.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( est_bugs[ANY]=bugs[CV()]*1.1 12 ) 13 / YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 110 1998 Windows98 130 143 2000 WindowsME 170 187 2002 WindowsXP 140 154 67
  • 68.
  • 69.
  • 70.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (department, year) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( est_bugs['HR', ANY]=bugs['HR', CV()]*1.1 12 ( est_bugs['Sales', ANY]=bugs['Sales', CV()]*1.2 13 ( est_bugs['Payroll', ANY]=bugs['Payroll', CV()]*1.5 14 ) 15 / not just numeric 70
  • 71.
  • 72.
  • 73.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model 7 dimension by (year) 8 measures (ver,bugs, 0 est_bugs) 9 rules 10 ( 11 ); YR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 1996 RedHat 50 0 2006 Oracle 80 0 2001 SUSE 65 0 2003 CentOS 90 0 73
  • 74.
    SQL> insert intoversions 2 values ('Linux',2001,'Fedora',70); 1 row created. SQL> select yr 2 ,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model 7 dimension by (year as yr) 8 measures (ver,bugs, 0 est_bugs) 9 rules 10 ( 11 ); from versions * ERROR at line 5: ORA-32638: Non unique addressing in MODEL dimensions 74
  • 75.
  • 76.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model UNIQUE SINGLE REFERENCE 7 dimension by (year as yr) 8 measures (ver,bugs, 0 est_bugs) 9 rules ( ); YR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 1996 RedHat 50 0 2006 Oracle 80 0 2001 SUSE 65 0 2003 CentOS 90 0 2001 Fedora 70 0 76
  • 77.
    unique single reference rulescannot cross dimensions 77
  • 78.
  • 79.
    SQL> select yr,ver,bugs,est_bugs 2from versions 3 where platform = 'Win' 4 model 5 dimension by (year as yr) 6 measures (ver,bugs, 0 est_bugs) 7 rules 8 ( 9 est_bugs[null]=140 10 ); YR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 (null) 140 79
  • 80.
  • 81.
    SQL> select yearas yr 2 ,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model 7 dimension by (yr) 8 measures (ver,bugs, 0 est_bugs) 9 rules 10 ( 11 ); dimension by (yr) * ERROR at line 7: ORA-00904: "YR": invalid identifier 81
  • 82.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model 7 dimension by (year as yr) 8 measures (ver,bugs, 0 est_bugs) 9 rules 10 ( 11 ); YR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 1996 RedHat 50 0 2006 Oracle 80 0 2001 SUSE 65 0 2003 CentOS 90 0 82
  • 83.
  • 84.
  • 85.
    SQL> select ... 7model 8 dimension by (year) 9 measures (ver, 10 bugs, 11 bugs*0.5 half_bugs, 12 avg(bugs) over ( 13 order by year rows 14 between 1 preceding 15 and 1 following ) est_bugs) 16 rules 17 ( 18 ); YEAR VER BUGS HALF_BUGS EST_BUGS ---------- ------------ ---------- ---------- ---------- 1995 Windows95 100 50 115 1998 Windows98 130 65 133.333333 2000 WindowsME 170 85 146.666667 2002 WindowsXP 140 70 155 85
  • 86.
  • 87.
    SQL> select platform,tot_bugs 2 from versions 3 group by platform 4 model 5 dimension by (platform) 6 measures (sum(bugs) as tot_bugs) 7 rules 8 ( 9 ); PLATFORM TOT_BUGS ---------- ---------- Linux 285 Win 540 87
  • 88.
  • 89.
    SQL> select platform,sum(bugs) as tot_bugs 2 from versions 3 group by platform 4 model 5 dimension by (platform) 6 measures (tot_bugs) 7 rules 8 ( 9 ); measures (tot_bugs) * ERROR at line 6: ORA-00904: "TOT_BUGS": invalid identifier 89
  • 90.
  • 91.
    SQL> select platform,year, ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs) 7 rules 8 ( ver[2009]='Windows7', 9 bugs[2009]=150 10 ); select platform, year, ver, bugs * ERROR at line 1: ORA-32614: illegal MODEL SELECT expression 91
  • 92.
    SQL> select platform,year, ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,platform) 7 rules 8 ( ver[2009]='Windows7', 9 bugs[2009]=150 10 ); PLATFORM YEAR VER BUGS ---------- ---------- --------------- ---------- Win 1995 Windows95 100 Win 1998 Windows98 130 Win 2000 WindowsME 170 Win 2002 WindowsXP 140 2009 Windows7 150 92
  • 93.
  • 94.
    SQL> select year,ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,year) 7 rules 8 ( ver[2009]='Windows7', 9 bugs[2009]=150, 10 year[2009]=2010 11 ); select year, ver, bugs * ERROR at line 1: ORA-00957: duplicate column name 94
  • 95.
    SQL> select year,ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,year next_year) 7 rules 8 ( ver[2009]='Windows7', 9 bugs[2009]=150, 10 next_year[2009]=2010 11 ); YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 2009 Windows7 150 95
  • 96.
    SQL> select year,ver, bugs, next_year 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,year next_year) 7 rules 8 ( ver[2009]='Windows7', 9 bugs[2009]=150, 10 next_year[2009]=2010 11 ); YEAR VER BUGS NEXT_YEAR ---------- --------------- ---------- ---------- 1995 Windows95 100 1995 1998 Windows98 130 1998 2000 WindowsME 170 2000 2002 WindowsXP 140 2002 2009 Windows7 150 2010 96
  • 97.
  • 98.
    SQL> select year,ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,null est_sales) 7 rules 8 ( 9 est_sales[ANY]=0 10 ); measures (ver,bugs,null est_sales) * ERROR at line 6: ORA-01723: zero-length columns are not allowed 98
  • 99.
    SQL> select year,ver, bugs 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,to_number(null) est_sales) 7 rules 8 ( 9 est_sales[ANY]=0 10 ); YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 99
  • 100.
    SQL> select year,ver, bugs, sales_rep 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs, 7 cast(null as varchar2(20)) sales_rep) 8 rules 9 ( 10 sales_rep[2000]='Mike' 11 ); YEAR VER BUGS SALES_REP ---------- --------------- ---------- -------------------- 1995 Windows95 100 1998 Windows98 130 2002 WindowsXP 140 2000 WindowsME 170 Mike 100
  • 101.
    SQL> select year,ver, bugs, sales_rep 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs,'Unknown' sales_rep) 7 rules 8 ( 9 sales_rep[2000]='Mike Jones' 10 ); ERROR: ORA-25137: Data value out of range 101
  • 102.
    SQL> select year,ver, bugs, sales_rep 2 from versions 3 where platform = 'Win' 4 model 5 dimension by (year) 6 measures (ver,bugs, 7 cast('Unknown' as varchar2(20)) sales_rep) 8 rules 9 ( 10 sales_rep[2000]='Mike Jones' 11 ); YEAR VER BUGS SALES_REP ---------- --------------- ---------- -------------------- 1995 Windows95 100 Unknown 1998 Windows98 130 Unknown 2002 WindowsXP 140 Unknown 2000 WindowsME 170 Mike Jones 102
  • 103.
  • 104.
  • 105.
    SQL> select platform ... 6from versions 7 model 8 partition by ( platform ) 9 dimension by (year) 10 measures (ver,bugs, 0 est_bugs) 11 rules 12 ( est_bugs[1995]=bugs[1995], 13 est_bugs[year > 1995]=bugs[CV()]*1.1 14 ); PLATFORM YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- Linux 1996 RedHat 50 55 Linux 2006 Oracle 80 88 Linux 2001 SUSE 65 71.5 Linux 2003 CentOS 90 99 Linux 1995 Win 1995 Windows95 100 100 Win 1998 Windows98 130 143 Win 2000 WindowsME 170 187 Win 2002 WindowsXP 140 154 105
  • 106.
    rules applied perpartition 106
  • 107.
  • 108.
  • 109.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[1998]=100, 13 bugs[2000]= :bindvar, 14 est_bugs[2009]= dbms_random.value(80,100) 15 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 2002 WindowsXP 140 0 1998 Windows98 130 100 2000 WindowsME <:bv> 0 2009 90.1501647 109
  • 110.
  • 111.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[2009]= 13 ( select max(bugs) from versions ) 14 ); select yr * ERROR at line 1: ORA-32620: illegal subquery within MODEL rules 111
  • 112.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs, 10 ( select max(bugs) from versions ) x) 11 rules 12 ( 13 est_bugs[2009]= x[2000] 14 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2002 WindowsXP 140 0 2000 WindowsME 170 0 2009 170 112
  • 113.
  • 114.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( est_bugs[ANY]=bugs[CV()]*1.1 12 ) 13 / YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 110 1998 Windows98 130 143 2000 WindowsME 170 187 2002 WindowsXP 140 154 114
  • 115.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs, 0 zero_bug) 10 rules 11 ( zero_bug[ANY]=CV()+10 12 ); ( zero_bug[ANY]=CV()+10 * ERROR at line 11: ORA-32611: incorrect use of MODEL CV operator 115
  • 116.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 ,zero_bug 6 from versions 7 where platform = 'Win' 8 model 9 dimension by (year) 10 measures (ver,bugs, 0 est_bugs, 0 zero_bug) 11 rules 12 ( zero_bug[ANY]=CV(YEAR)+10 13 ); YEAR VER BUGS EST_BUGS ZERO_BUG ---------- ------------ ---------- ---------- ---------- 1995 Windows95 100 0 2005 1998 Windows98 130 0 2008 2000 WindowsME 170 0 2010 2002 WindowsXP 140 0 2012 116
  • 117.
  • 118.
    SQL> select fib 2from ( select rownum r from dual 3 connect by level <= 10 ) 4 model 5 dimension by (r) 6 measures (0 fib) 7 rules 8 ( fib[1]=1, 9 fib[2]=1, 10 fib[r>2]=fib[CV()-1]+fib[CV()-2] ); FIB ---------- 1 1 2 3 5 8 13 21 34 55 118
  • 119.
  • 120.
  • 121.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[2009]=100, 13 ver[2009]='Windows7' 14 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 2009 Windows7 100 update or create a dimension value of '2009' 121
  • 122.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[yr=2009]=100, 13 ver[yr=2009]='Windows7' 14 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 update if exists a dimension value of '2009' 122
  • 123.
  • 124.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[yr IN (2000,2009)]=100 13 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2002 WindowsXP 140 0 2000 WindowsME 170 100 124
  • 125.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[FOR yr IN (2000,2009)]=100 13 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2002 WindowsXP 140 0 2000 WindowsME 170 100 2009 100 125
  • 126.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[FOR yr FROM 2002 TO 2005 INCREMENT 1]=100 13 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 100 2003 100 2004 100 2005 100 126
  • 127.
    SQL> select ... 7model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[FOR yr IN 13 ( select year 14 from versions 15 where platform = 'Linux') ]=100 16 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2000 WindowsME 170 0 2002 WindowsXP 140 0 1996 100 2006 100 2001 100 2003 100 127
  • 128.
  • 129.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[FOR yr IN (2000,2009)]=100, 13 ver[2009]='Windows7' ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2002 WindowsXP 140 0 2000 WindowsME 170 100 2009 Windows7 100 UPSERT 129
  • 130.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules UPDATE 11 ( 12 est_bugs[FOR yr IN (2000,2009)]=100, 13 ver[2009]='Windows7' ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 0 2002 WindowsXP 140 0 2000 WindowsME 170 100 2009 Windows7 100 2009 gone 130
  • 131.
    idiosyncracies # 2 the"sert" in upsert 131
  • 132.
    SQL> select * 2from versions 3 where platform = 'z/OS'; no rows selected SQL> select * 2 from versions 3 where platform = 'z/OS' 4 model 5 dimension by (year as yr) 6 measures (ver, 0 est_bugs) 7 rules 8 ( est_bugs[FOR yr IN (2000,2002,2009)]=100, 9 ver[ANY]='IBM' ); YR VER EST_BUGS ---------- --------------- ---------- 2000 IBM 100 2002 IBM 100 2009 IBM 100 132
  • 133.
  • 134.
  • 135.
    SQL> select yr 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs, year as yr2) 10 rules 11 ( 12 yr2[ANY]=yr2[CV()]-2, 13 est_bugs[yr2[2000]]=100 14 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 0 1998 Windows98 130 100 2000 WindowsME 170 0 2002 WindowsXP 140 0 yr2[2000]=2000-2=1998 est_bugs[1998]=100 135
  • 136.
    most typical SQLapplies 136
  • 137.
    SQL> select ... 7model 8 dimension by (year as yr) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs[yr is null]=100, 13 est_bugs[yr is not null]=110, 14 est_bugs[yr > 0]=120, 15 est_bugs[yr between 2000 and 2010]=130, 16 est_bugs[null]=140 17 ); YR VER BUGS EST_BUGS ---------- ------------ ---------- ---------- 1995 Windows95 100 120 1998 Windows98 130 120 2000 WindowsME 170 130 2002 WindowsXP 140 130 (null) 140 137
  • 138.
  • 139.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 model 7 dimension by (platform,year) 8 measures (ver,bugs, 0 est_bugs) 9 rules 10 ( 11 est_bugs[2009]=100 12 ); est_bugs[2009]=100 * ERROR at line 11: ORA-00947: not enough values 139
  • 140.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( 12 est_bugs['Win',2009]=100 13 ); est_bugs['Win',2009]=100 * ERROR at line 12: ORA-00947: not enough values 140
  • 141.
  • 142.
    SQL> select year 2,ver 3 ,bugs 4 ,cumtot 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 cumtot) 10 rules 11 ( cumtot[ANY]=SUM(bugs)[year <= CV(year)] 12 ); YEAR VER BUGS CUMTOT ---------- ------------ ---------- ---------- 1995 Windows95 100 100 1998 Windows98 130 230 2000 WindowsME 170 400 2002 WindowsXP 140 540 142
  • 143.
    SQL> select year 2,ver 3 ,bugs 4 ,wt 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 wt) 10 rules 11 ( wt[ANY]=bugs[CV()]* 12 row_number() over ( order by year ) 13 ); YEAR VER BUGS WT ---------- ------------ ---------- ---------- 1995 Windows95 100 100 1998 Windows98 130 260 2000 WindowsME 170 510 2002 WindowsXP 140 560 143
  • 144.
  • 145.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules automatic order 10 ( bugs[2000]=150, 11 bugs[ANY]=bugs[CV()]*1.1 12 ) 13 / bugs[ANY]=bugs[CV()]*1.1 * ERROR at line 11: ORA-32630: multiple assignment in automatic order MODEL 145
  • 146.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules sequential order 10 ( bugs[2000]=150, 11 bugs[ANY]=bugs[CV()]*1.1 12 ) 13 / YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 110 1998 Windows98 143 2000 WindowsME 165 2002 WindowsXP 154 146
  • 147.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model 7 dimension by (year) 8 measures (ver,bugs) 9 rules sequential order 10 ( bugs[ANY]=bugs[CV()]*1.1, 11 bugs[2000]=150 12 ) 13 / YEAR VER BUGS ---------- --------------- ---------- 1995 Windows95 110 1998 Windows98 143 2000 WindowsME 150 2002 WindowsXP 154 110 143 165 154 147
  • 148.
  • 149.
    SQL> select year 2,ver 3 ,bugs 4 from versions 5 where platform = 'Win' 6 model return updated rows 7 dimension by (year) 8 measures (ver,bugs) 9 rules 10 ( ver[2009]='Windows7', 11 bugs[2009]=150 12 ) 13 / YEAR VER BUGS ---------- --------------- ---------- 2009 Windows7 150 149
  • 150.
  • 151.
  • 152.
    SQL> select year 2,ver 3 ,bugs 4 ,est_bugs 5 from versions 6 where platform = 'Win' 7 model 8 dimension by (year) 9 measures (ver,bugs, 0 est_bugs) 10 rules 11 ( est_bugs[ANY]=bugs[CV()-1]*1.1 12 ) 13 / YEAR VER BUGS EST_BUGS ---------- --------------- ---------- ---------- 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 est_bugs[1995]=bugs[1994] est_bugs[1998]=bugs[1997] est_bugs[2000]=bugs[1999] est_bugs[2002]=bugs[2001] 152
  • 153.
  • 154.
    SQL> select rank()over ( order by year) as year_rank 2 ,year 3 ,ver 4 ,bugs 5 from versions 6 where platform = 'Win' 7 / YEAR_RANK YEAR VER BUGS ---------- ---------- --------------- ---------- 1 1995 Windows95 100 2 1998 Windows98 130 3 2000 WindowsME 170 4 2002 WindowsXP 140 154
  • 155.
    SQL> select year_rank,year 2,ver,bugs,est_bugs 3 from versions 4 where platform = 'Win' 5 model 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,0 est_bugs) 9 rules 10 ( est_bugs[1]=bugs[1], 11 est_bugs[year_rank > 1]=bugs[CV()-1]*1.1 12 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 100 2 1998 Windows98 130 110 3 2000 WindowsME 170 143 4 2002 WindowsXP 140 187 155
  • 156.
  • 157.
  • 158.
  • 159.
    SQL> select year_rank,year 2,ver,bugs,est_bugs 3 from versions 4 where platform = 'Win' 5 model 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,0 est_bugs) 9 rules 10 ( est_bugs[1]=bugs[1], 11 est_bugs[year_rank > 1]=bugs[CV()-1]*1.1 12 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 100 2 1998 Windows98 130 110 3 2000 WindowsME 170 143 4 2002 WindowsXP 140 187 159
  • 160.
    SQL> select year_rank,year 2,ver,bugs,est_bugs 3 from versions 4 where platform = 'Win' 5 model 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,0 est_bugs) 9 rules 10 ( est_bugs[ANY]=bugs[CV()-1]*1.1 11 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 2 1998 Windows98 130 110 3 2000 WindowsME 170 143 4 2002 WindowsXP 140 187 160
  • 161.
    but can beoverridden 161
  • 162.
    SQL> select year_rank,year 2,ver,bugs,est_bugs 3 from versions 4 where platform = 'Win' 5 model IGNORE NAV 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,0 est_bugs) 9 rules 10 ( est_bugs[ANY]=bugs[CV()-1]*1.1 11 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 0 2 1998 Windows98 130 110 3 2000 WindowsME 170 143 4 2002 WindowsXP 140 187 162
  • 163.
    SQL> select * 2from dual 3 where 1=0 4 model ignore nav 5 dimension by (0 tag) 6 measures (to_date(null) d, 7 to_number(null) n, 8 cast(null as varchar2(10)) v) 9 rules 10 ( d[0] = d[1], 11 n[0] = n[1], 12 v[0] = v[1] 13 ); TAG D N V ---------- ------------------- ---------- ------ 0 01/01/2000 00:00:00 0 dimension 1 does not exist beware documentation ! 163
  • 164.
  • 165.
    SQL> select year_rank,year 2,ver,bugs,cum_bugs 3 from versions 4 where platform = 'Win' 5 model 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,to_number(null) cum_bugs) 9 rules 10 ( cum_bugs[ANY]=cum_bugs[CV()-1]+bugs[CV()-1] 11 ); YEAR_RANK YEAR VER BUGS CUM_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 2 1998 Windows98 130 3 2000 WindowsME 170 4 2002 WindowsXP 140 165
  • 166.
    SQL> select year_rank,year 2,ver,bugs,cum_bugs 3 from versions 4 where platform = 'Win' 5 model IGNORE NAV 6 dimension by ( 7 rank() over ( order by year) as year_rank) 8 measures (ver,bugs,year,to_number(null) cum_bugs) 9 rules 10 ( cum_bugs[ANY]=cum_bugs[CV()-1]+bugs[CV()-1] 11 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- --------------- ---------- ---------- 1 1995 Windows95 100 0 2 1998 Windows98 130 100 3 2000 WindowsME 170 230 4 2002 WindowsXP 140 400 166
  • 167.
  • 168.
    SQL> select ... 6from versions 7 where platform = 'Win' 8 model 9 dimension by (rank() over ( order by year) as year_rank) 10 measures (ver,bugs,year,bugs est_bugs) 11 rules 12 ( 13 est_bugs[ANY]=est_bugs[CV()-1]*1.1 14 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- ------------ ---------- ---------- 1 1995 Windows95 100 2 1998 Windows98 130 3 2000 WindowsME 170 4 2002 WindowsXP 140 168
  • 169.
    SQL> select ... 6from versions 7 where platform = 'Win' 8 model 9 dimension by (rank() over ( order by year) as year_rank) 10 measures (ver,bugs,year,bugs est_bugs) 11 rules 12 ( 13 est_bugs[ANY] order by year_rank desc = 14 est_bugs[CV()-1]*1.1 15 ); YEAR_RANK YEAR VER BUGS EST_BUGS ---------- ---------- ------------ ---------- ---------- 1 1995 Windows95 100 2 1998 Windows98 130 110 3 2000 WindowsME 170 143 4 2002 WindowsXP 140 187 169
  • 170.
    lots of null"variations" column is null dimension does not exist ignore nav / keep nav 170
  • 171.
    SQL> select year,ver, bugs, patches 2 from versions 3 where platform = 'Win'; YEAR VER BUGS PATCHES ---------- --------------- ---------- ---------- 1994 Windows3.1.1 140 1995 Windows95 100 1998 Windows98 130 2000 WindowsME 170 2002 WindowsXP 140 3 2003 Windows2003 140 1 FCAST ---------- (null) (null) (null) (null) (null) 110 "forecast = bugs(year-1) – 10*patches(year-1)" 171
  • 172.
    SQL> select year,ver, patches, fcast, pv, pnv 2 from versions 3 where platform = 'Win' 4 model 5 dimension by ( year ) 6 measures (ver, bugs, 0 fcast, patches, 'X' pv, 'X' pnv) 7 rules 8 ( 9 fcast[ANY]=bugs[CV()]-10*patches[CV()-1], 10 pv[ANY]=PRESENTV( patches[CV()-1], 'Y','N'), 11 pnv[ANY]=PRESENTNNV( patches[CV()-1], 'Y','N') 12 ); YEAR VER PATCHES FCAST PV PNV ---------- --------------- ---------- ---------- ---- ---- 1994 Windows3.1.1 N N 1995 Windows95 Y N 1998 Windows98 N N 2000 WindowsME N N 2002 WindowsXP 3 N N 2003 Windows2003 1 110 Y Y 172
  • 173.
    "duh...I know aboutNVL" resultset time rule execution time 173
  • 174.
  • 175.
    "if the numberof bugs drops by 4% each year, what is the 5 year forecast for each product" Windows XP 140 Windows XP 134 Windows XP 129 Windows XP 123 Windows XP 118 Windows XP 113 175
  • 176.
    SQL> select year,ver, bugs, bugs5yr 2 from versions 3 where platform = 'Win' 4 model 5 dimension by ( year ) 6 measures (ver, bugs, bugs bugs5yr) 7 rules ITERATE(5) 8 ( 9 bugs5yr[ANY]=bugs5yr[CV()] * 0.96 10 ); YEAR VER BUGS BUGS5YR ---------- --------------- ---------- ------- 1995 Windows95 100 81.54 1998 Windows98 130 106.00 2000 WindowsME 170 138.61 2002 WindowsXP 140 114.15 176
  • 177.
    5 iterations perpartition 177
  • 178.
  • 179.
    SQL> select num 2from dual 3 model 4 dimension by ( 0 x) 5 measures ( 0 num) 6 rules iterate (20) 7 ( 8 num[ITERATION_NUMBER]=ITERATION_NUMBER 9 ); NUM ---------- 0 1 2 3 4 ... 17 18 19 20 rows selected. starts from zero 179
  • 180.
  • 181.
  • 182.
    SQL> select * 2from loans; DESCR YEAR BAL RATE PMT ---------- ---------- ---------- ---------- ---------- Car 2000 25000 1.13 350 House 2010 300000 1.04 2200 182
  • 183.
    SQL> select descr,bal final_bal, yrs 2 from loans 3 model 4 partition by ( year) 5 dimension by (0 idx) 6 measures (descr, bal, rate, pmt, 0 yrs) 7 rules iterate (50) until bal[0]*rate[0]-12*pmt[0]<0 8 ( 9 bal[0]=bal[0]*rate[0]-12*pmt[0] 10 ,yrs[0]=iteration_number 11 ); DESCR FINAL_BAL YRS ---------- ----------- ---------- House 11660.34 14 Car 632.33 11 183
  • 184.
  • 185.
    "how many yearswill it take for my house to appreciate in value more than my annual mortgage payments" 185
  • 186.
    SQL> select year,bal, apprec, yrs 2 from loans 3 where descr = 'House' 4 model 5 partition by ( year ) 6 dimension by ( 0 idx) 7 measures ( year y, bal, 12*pmt mort, apprec, 0 yrs) 8 rules iterate (20) 9 until bal[0]- previous( bal[0] ) > mort[0] 10 ( 11 bal[0]=bal[0]*(1+apprec[0]/100), 12 yrs[0]=iteration_number+1 13 ); YEAR BAL APPREC YRS ---------- ---------- --------- ---------- 2010 437742.689 6.5 6 186
  • 187.
  • 188.
    SQL> select *from VERSIONS; PLATFORM YEAR VER BUGS ---------- ---------- --------------- ---------- Win 1995 Windows95 100 Win 1998 Windows98 130 Win 2000 WindowsME 170 Win 2002 WindowsXP 140 Linux 1996 RedHat 50 Linux 2006 Oracle 80 Linux 2001 SUSE 65 Linux 2003 CentOS 90 SQL> select * from PLATFORM_TYPE; PLATFORM OWNERSHIP -------------------- -------------------- Win Private Linux Open Source 188
  • 189.
    SQL> select year,ver, bugs, est_bugs, own_ship 2 from versions 3 model 4 reference pt on 5 ( select platform p, ownership own 6 from platform_type ) 7 dimension by (p) 8 measures (own) 9 dimension by (year) 10 measures (ver,bugs, 0 est_bugs, 11 platform, cast(null as varchar2(20)) own_ship) 12 rules 13 ( est_bugs[1995]=bugs[1995], 14 est_bugs[year > 1995]=bugs[cv()]*1.1, 15 own_ship[any]=pt.own[platform[cv()]] 16 ); YEAR VER BUGS EST_BUGS OWN_SHIP ---------- --------------- ---------- ---------- ----------- 1995 Windows95 100 100 Private 1998 Windows98 130 143 Private 2000 WindowsME 170 187 Private 2002 WindowsXP 140 154 Private 1996 RedHat 50 55 Open Source 2006 Oracle 80 88 Open Source 2001 SUSE 65 71.5 Open Source 2003 CentOS 90 99 Open Source189
  • 190.
    why not justa join ? 190
  • 191.
    “imagination is more importantthan knowledge” - Albert Einstein 191
  • 192.
  • 193.
    SQL> create tableDEC as 2 select rownum r 3 from dual connect by level < 1000; Table created. SQL> select * from DEC; R ---------- 1 2 3 4 5 6 7 8 9 10 193
  • 194.
    SQL> select r,ltrim(str,'0') 2 from DEC 3 model 4 dimension by (r) 5 measures ( 6 cast('' as varchar2(64)) str, 7 r as runtot, 8 power(2,32) as chk) 9 rules iterate(33) 10 ( str[ANY] = 11 str[CV()] || 12 case 13 when bitand(runtot[CV()],chk[CV()]) > 0 14 then '1' else '0' 15 end, 16 runtot[ANY] = 17 runtot[CV()] - 18 bitand(runtot[CV()],chk[CV()]), 19 chk[ANY] = chk[CV()] / 2 20 ); 194
  • 195.
    R BITSTR -------- ----------------- 11 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 16 10000 17 10001 18 10010 195
  • 196.
  • 197.
    select r, ltrim(str,'0') fromDEC model partition by (r) dimension by (1 blah) measures ( cast('' as varchar2(64)) str, r as runtot, power(2,32) as chk) rules iterate(33) ( str[1] = str[1] || case when bitand(runtot[1],chk[1]) > 0 then '1' else '0' end, runtot[1] = runtot[1] - bitand(runtot[1],chk[1]), chk[1] = chk[1] / 2 ); 197
  • 198.
  • 199.
    SQL> select e1.ename,e2.ename manager, e1.sal, e1.comm 2 from emp e1, emp e2 3 where e1.mgr= e2.empno; ENAME MANAGER SAL COMM ---------- ---------- ---------- ---------- JONES KING 2975 CLARK KING 2450 BLAKE KING 2850 WARD BLAKE 1250 500 JAMES BLAKE 950 TURNER BLAKE 1500 0 ALLEN BLAKE 1600 300 MARTIN BLAKE 1250 1400 199
  • 200.
    SQL> select ename,manager, sal, comm 2 from emp 3 model 4 dimension by (empno) 5 measures (ename, mgr, sal, comm, 6 cast (null as varchar2(10)) as manager) 7 rules ( 8 manager[any] = ename[mgr[cv()]] 9 ); ENAME MANAGER SAL COMM ---------- ---------- ---------- ---------- KING ALLEN 5000 BLAKE KING 2850 CLARK KING 2450 JONES KING 2975 MARTIN BLAKE 1250 1400 ALLEN BLAKE 1600 300 ... 200
  • 201.
  • 202.
    SQL> select deptno,ltrim(str,',') str 2 from emp 3 model return updated rows 4 partition by (deptno) 5 dimension by ( 6 rank() over ( partition by deptno order by empno) x ) 7 measures ( ename, cast('' as varchar2(4000)) str) 8 rules iterate (20) 9 until presentv(ename[iteration_number+1],1,2)=2 10 ( 11 str[1] = str[1] || ','|| ename[iteration_number+1] 12 ); DEPTNO STR ---------- ------------------------------------------------- 20 SMITH,JONES,SCOTT,ADAMS,FORD, 10 CLARK,KING,MILLER, 30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES, 202
  • 203.
  • 204.
    SQL> select 2 col1, 3col2, 4 my_function(...) 5 from EMP; 204
  • 205.
  • 206.
  • 207.
  • 208.
    SELECT c, p,m, pp, ip FROM MORTGAGE MODEL REFERENCE R ON (SELECT customer, fact, amt FROM mortgage_facts MODEL DIMENSION BY (customer, fact) MEASURES (amount amt) RULES (amt[any, 'PaymentAmt']= (amt[CV(),'Loan']* Power(1+ (amt[CV(),'Annual_Interest']/100/12), amt[CV(),'Payments']) * (amt[CV(),'Annual_Interest']/100/12)) / (Power(1+(amt[CV(),'Annual_Interest']/100/12), amt[CV(),'Payments']) - 1))) DIMENSION BY (customer cust, fact) measures (amt) MAIN amortization PARTITION BY (customer c) DIMENSION BY (0 p) MEASURES (principalp pp, interestp ip, mort_balance m, customer mc) RULES ITERATE(1000) UNTIL (ITERATION_NUMBER+1 =r.amt[mc[0],'Payments']) (ip[ITERATION_NUMBER+1] = m[CV()-1] * r.amt[mc[0], 'Annual_Interest']/1200, pp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'] - ip[CV()], m[ITERATION_NUMBER+1] = m[CV()-1] - pp[CV()] ) 208
  • 209.
  • 210.
  • 211.
    you cannot killExcel 211
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.