Download presentation
Presentation is loading. Please wait.
Published byChloe Elliott Modified over 9 years ago
1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 1
2
5 of the Best Things to Happen to SQL Thomas Kyte http://asktom.oracle.com
3
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 of the corporate presentation template 3 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Small print
4
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 4 #1 Analytics
5
Analytics Ordered Array Semantics in SQL queries Deptno Ename Sal Select deptno,ename,sal from emp 10 20 30 over (partition by deptno King 5000 Clark 2450 Miller 1300 Order by sal desc ) 123123 Row_number() SCOTT3000 FORD3000 JONES2975 ADAMS1100 SMITH800 1234512345
6
Why Analytics A running total (demo001.sql) Percentages within a group (demo002.sql) Top-N queries (demo003.sql) Moving Averages (demo004.sql) Ranking Queries (demo005.sql) Medians (med.sql) And the list is infinitely long – "Analytics are the coolest thing to happen to SQL since the keyword Select"
7
Why Analytics scott%ORA12CR1> Select deptno, ename, sal, 2 sum(sal) over (partition by deptno order by sal) running_total1, 3 sum(sal) over (partition by deptno order by sal, rowid) running_total2 4 from emp order by deptno, sal; DEPTNO ENAME SAL RUNNING_TOTAL1 RUNNING_TOTAL2 ---------- ---------- ---------- -------------- -------------- 10 MILLER 1300 1300 1300 CLARK 2450 3750 3750 KING 5000 8750 8750 20 SMITH 800 800 800 ADAMS 1100 1900 1900 JONES 2975 4875 4875 SCOTT 3000 10875 7875 FORD 3000 10875 10875 30 JAMES 950 950 950 WARD 1250 3450 2200
8
Why Analytics scott%ORA12CR1> select deptno, ename, sal, 2 to_char( round( 3 ratio_to_report(sal) over (partition by deptno) 4 *100, 2 ), '990.00' )||'%' rtr 5 from emp 6 order by deptno, sal 7 / DEPTNO ENAME SAL RTR ---------- ---------- ---------- -------- 10 MILLER 1300 14.86% CLARK 2450 28.00% KING 5000 57.14% 20 SMITH 800 7.36% ADAMS 1100 10.11% JONES 2975 27.36% SCOTT 3000 27.59% FORD 3000 27.59%
9
Why Analytics scott%ORA12CR1> select emp.deptno, ename, sal, 2 to_char( round( 3 sal/tot_dept 4 *100, 2 ), '990.00' )||'%' rtr 5 from emp, (select deptno, sum(sal) tot_dept from emp group by deptno) d 6 where emp.deptno = d.deptno 7 order by deptno, sal 8 / DEPTNO ENAME SAL RTR ---------- ---------- ---------- -------- 10 MILLER 1300 14.86% CLARK 2450 28.00% KING 5000 57.14% 20 SMITH 800 7.36% ADAMS 1100 10.11% JONES 2975 27.36% SCOTT 3000 27.59% FORD 3000 27.59%
10
Why Analytics scott%ORA12CR1> select deptno, ename, sal, rn 2 from ( 3 Select deptno, ename, sal, 4 row_number() over (partition by deptno order by sal desc) rn 5 from emp 6 ) 7 where rn <= 3 8 / DEPTNO ENAME SAL RN ---------- ---------- 10 KING 5000 1 CLARK 2450 2 MILLER 1300 3 20 SCOTT 3000 1 FORD 3000 2 JONES 2975 3
11
Why Analytics scott%ORA12CR1> select deptno, ename, sal, rank 2 from ( 3 Select deptno, ename, sal, 4 rank() over (partition by deptno order by sal desc) rank 5 from emp 6 ) 7 where rank <= 3 8 / DEPTNO ENAME SAL RANK ---------- ---------- 10 KING 5000 1 CLARK 2450 2 MILLER 1300 3 20 SCOTT 3000 1 FORD 3000 1 JONES 2975 3
12
Why Analytics scott%ORA12CR1> select deptno, ename, sal, dr 2 from ( 3 Select deptno, ename, sal, 4 dense_rank() over (partition by deptno order by sal desc) dr 5 from emp 6 ) 7 where dr <= 3 8 / DEPTNO ENAME SAL DR ---------- ---------- 10 KING 5000 1 CLARK 2450 2 MILLER 1300 3 20 SCOTT 3000 1 FORD 3000 1 JONES 2975 2 ADAMS 1100 3
13
Why Analytics scott%ORA12CR1> Select ename, sal, 2 round( 3 avg(sal) over (order by sal 4 rows 5 preceding) 5 ) avg_sal, 6 rtrim( 7 lag(sal) over (order by sal) || ',' || 8 lag(sal,2) over (order by sal) || ',' || 9 lag(sal,3) over (order by sal) || ',' || 10 lag(sal,4) over (order by sal) || ',' || 11 lag(sal,5) over (order by sal),',') last_5 12 from emp 13 order by sal; ENAME SAL AVG_SAL LAST_5 ---------- ---------- ---------- ------------------------------ SMITH 800 800 JAMES 950 875 800 ADAMS 1100 950 950,800 WARD 1250 1025 1100,950,800 MARTIN 1250 1070 1250,1100,950,800 MILLER 1300 1108 1250,1250,1100,950,800 …
14
Why Analytics ops$tkyte%ORA12CR1> select object_type, round( avg( diff ), 2 ) avg_diff 2 from ( 3 select object_type, 4 created - lag(created) over (partition by object_type order by created) diff 5 from all_objects 6 ) 7 group by object_type 8 order by 2 desc 9 / OBJECT_TYPE AVG_DIFF ------------------- ---------- RULE LOB EDITION 390.21 MATERIALIZED VIEW 90.34 CLUSTER 42.89 DIRECTORY 13.9
15
Analytics To Tune insert into t ( empno, end_date, … ) select … from t1, t2, t3, t4,.... where …; loop delete from t where (empno,end_date) in ( select empno, min(end_date) from t group by empno having count(*) > 1 ); exit when sql%rowcount = 0; end loop; In short, keep the “newest” record for a given EMPNO. If there is just one such “newest” record…
16
Analytics insert /*+ APPEND */ into t ( empno, end_date,.... ) select … from ( select empno, end_date,...., max(end_date) OVER ( partition by empno ) max_end_date --count(end_date) OVER ( partition by empno,end_date ) c from t1, t2, t3, t4,.... where.... ) where end_date = max_end_date --and cnt = 1; The query took the same amount of time, the delete went from 3/6/9 hours to 0
17
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 17 #2 Pipelined Functions
18
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 18 Pipelined Functions SQL> create or replace 2 directory data_dir as '/tmp/' 3 / Directory created. SQL> create table external_table 2 (EMPNO NUMBER(4), … 10 ) 11 ORGANIZATION EXTERNAL 12 (type oracle_loader default directory data_dir 14 access parameters (fields terminated by ',') 16 location ('emp.dat') 17 ) 18 / Table created.
19
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 19 Pipelined Functions SQL> create or replace type 2 emp_scalar_type as object 3 (EMPNO NUMBER(4), … 10 DEPTNO NUMBER(2) 11 ) 12 / Type created. SQL> create or replace type 2 emp_table_type as table 3 of emp_scalar_type 4 / Type created.
20
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 20 Pipelined Functions create or replace function emp_etl ( p_cursor in sys_refcursor ) return emp_table_type PIPELINED as type array is table of external_table%rowtype; l_rec array; begin loop fetch p_cursor bulk collect into l_rec limit 100; for i in 1..l_rec.count loop -- whatever code pipe row( emp_scalar_type ( l_rec(i).empno, LOWER(l_rec(i).ename), l_rec(i).job, l_rec(i).mgr, l_rec(i).hiredate, l_rec(i).sal, l_rec(i).comm, l_rec(i).deptno ) ); end loop; exit when (p_cursor%notfound); end loop; return; end;
21
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 21 Pipelined Functions SQL> merge into EMP e1 2 using (select * 3 from TABLE 4 (emp_etl( 5 cursor(select * 6 from external_table)) 7 ) 8 ) e2 9 on (e2.empno = e1.empno) 10 when matched then 11 update set e1.sal = e2.sal, 12 e1.ename = e2.ename 13 when not matched then 14 insert (empno, ename, job, mgr, 15 hiredate, sal, comm, deptno) 16 values (e2.empno, e2.ename, 17 e2.job, e2.mgr, 18 e2.hiredate, e2.sal, 19 e2.comm, e2.deptno) 20 / 14 rows merged.
22
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 22 Cardinality Estimates ops$tkyte@ora12cr1> create or replace 2 function str2tbl( p_str in varchar2, p_delim in varchar2 default ',' ) 3 return sys.odciVarchar2List 4 PIPELINED 5 as 6 l_str long default p_str || p_delim; 7 l_n number; 8 begin 9 loop 10 l_n := instr( l_str, p_delim ); 11 exit when (nvl(l_n,0) = 0); 12 pipe row( ltrim(rtrim(substr(l_str,1,l_n-1))) ); 13 l_str := substr( l_str, l_n+1 ); 14 end loop; 15 end; 16 / Function created.
23
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 23 Cardinality Estimates ops$tkyte@ora12cr1> variable x varchar2(15) ops$tkyte@ora12cr1> exec :x := '1,2,3,a,b,c' PL/SQL procedure successfully completed. ops$tkyte@ora12cr1> select * from table(str2tbl(:x)); COLUMN_VALUE ------------------------------ 1 2 3 a b c 6 rows selected.
24
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 24 Cardinality Estimates ops$tkyte@ora12cr1> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------ SQL_ID ddk1tv9s5pzq5, child number 0 ------------------------------------- select * from table(str2tbl(:x)) Plan hash value: 2407808827 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 29 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 8168 | 16336 | 29 (0)| 00:00:01 | ---------------------------------------------------------------------------------------------
25
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 25 Cardinality Estimates ops$tkyte@ora12cr1> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT --------------------------------------------------------------------------------------------- ------- SQL_ID bd2f8rh30z3ww, child number 0 ------------------------------------- select /*+ cardinality(sq 10) */ * from table(str2tbl(:x)) sq Plan hash value: 2407808827 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 29 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 10 | 20 | 29 (0)| 00:00:01 | --------------------------------------------------------------------------------------------- 13 rows selected.
26
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 26 Cardinality Estimates ops$tkyte@ora12cr1> select 10/8168 from dual; 10/8168 ----------.00122429 select /*+ opt_estimate(table, sq, scale_rows=0.00122429) */ * from table(str2tbl(:x)) sq Plan hash value: 2407808827 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 29 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 10 | 20 | 29 (0)| 00:00:01 | ---------------------------------------------------------------------------------------------
27
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 27 Cardinality Estimates ops$tkyte@ora12cr1> CREATE OR REPLACE TYPE str2tbl_stats 2 AS OBJECT 3 ( 4 x NUMBER, 5 6 STATIC FUNCTION ODCIGetInterfaces 7 ( p_interfaces OUT SYS.ODCIObjectList 8 ) RETURN NUMBER, 9 10 STATIC FUNCTION ODCIStatsTableFunction 11 ( p_function IN SYS.ODCIFuncInfo, 12 p_stats OUT SYS.ODCITabFuncStats, 13 p_args IN SYS.ODCIArgDescList, 14 p_str IN varchar2 default NULL, 15 p_delim IN varchar2 default ',' 16 ) RETURN NUMBER 17 ); 18 / Type created.
28
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 28 Cardinality Estimates ops$tkyte@ora12cr1> CREATE or replace TYPE BODY str2tbl_stats 2 AS 3 4 STATIC FUNCTION ODCIGetInterfaces ( 5 p_interfaces OUT SYS.ODCIObjectList 6 ) RETURN NUMBER IS 7 BEGIN 8 p_interfaces := 9 SYS.ODCIObjectList( SYS.ODCIObject ('SYS', 'ODCISTATS2')); 10 RETURN ODCIConst.success; 11 END ODCIGetInterfaces; 12 13 STATIC FUNCTION ODCIStatsTableFunction ( 14 p_function IN SYS.ODCIFuncInfo, 15 p_stats OUT SYS.ODCITabFuncStats, 16 p_args IN SYS.ODCIArgDescList, 17 p_str IN varchar2 default NULL, 18 p_delim IN varchar2 default ',' 19 ) RETURN NUMBER IS 20 BEGIN 21 p_stats := SYS.ODCITabFuncStats 22 ( nvl( length(p_str)-length(replace(p_str,p_delim,''))+1, 10) ); 23 RETURN ODCIConst.success; 24 END ODCIStatsTableFunction; 25 END; 26 /
29
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 29 Cardinality Estimates ops$tkyte@ora12cr1> associate statistics with functions str2tbl using str2tbl_stats; Statistics associated.
30
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 30 Cardinality Estimates Select * from table ( str2tbl( :x||'',',') ) Plan hash value: 2407808827 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 29 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 10 | 20 | 29 (0)| 00:00:01 | ---------------------------------------------------------------------------------------------
31
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 31 Cardinality Estimates select /*+ dynamic_sampling( sq, 2 ) */ * from table( str2tbl(:x,',') ) sq Plan hash value: 2407808827 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 11 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 6 | 12 | 11 (0)| 00:00:01 | --------------------------------------------------------------------------------------------- Note ----- - dynamic sampling used for this statement (level=2) (must be hinted)
32
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 32 Cardinality Estimates with sq as (select /*+ materialize */ * from table( str2tbl( :x ) ) ) select * from sq Plan hash value: 630596523 ----------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 32 (100)| | | 1 | TEMP TABLE TRANSFORMATION | | | | | | | 2 | LOAD AS SELECT | | | | | | | 3 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 8168 | 16336 | 29 (0)| 00:00:01 | | 4 | VIEW | | 8168 | 135K| 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6652_20F4CB | 8168 | 16336 | 3 (0)| 00:00:01 | ----------------------------------------------------------------------------------------------------------------- 18 rows selected.
33
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 33 Cardinality Estimates with sq as (select /*+ materialize */ * from table( str2tbl( :x ) ) ) select * from sq Plan hash value: 630596523 ----------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 32 (100)| | | 1 | TEMP TABLE TRANSFORMATION | | | | | | | 2 | LOAD AS SELECT | | | | | | | 3 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 8168 | 16336 | 29 (0)| 00:00:01 | | 4 | VIEW | | 6 | 102 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6653_20F4CB | 6 | 12 | 3 (0)| 00:00:01 | ----------------------------------------------------------------------------------------------------------------- Note ----- - cardinality feedback used for this statement 22 rows selected.
34
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 34 #3 External Tables
35
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 35 External Tables Query flat files Query output of programs (10.2.0.5 and above) – Load compressed files without uncompressing – Query program output, like ls, ps, df, etc Infinite possibilities Sqlldr is the legacy data loading tool from the 20 th century
36
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 36 External Tables SQL> create or replace directory exec_dir as '/home/tkyte/df‘; Directory created. SQL> !cat /home/tkyte/df #!/bin/bash /bin/df –Pl SQL> !/home/tkyte/run_df.sh Filesystem 1024-blocks Used Available Capacity Mounted on /dev/mapper/VolGr... 18156292 10827600 6391528 63% / /dev/sda1 101086 12062 83805 13% /boot tmpfs 517520 0 517520 0% /dev/shm
37
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 37 External Tables SQL> create table df 2 ( 3 fsname varchar2(100), 4 blocks number, 5 used number, 6 avail number, 7 capacity varchar2(10), 8 mount varchar2(100) 9 ) 10 organization external 11 ( 12 type oracle_loader 13 default directory exec_dir 14 access parameters 15 ( 16 records delimited 17 by newline 18 preprocessor 19 exec_dir:'run_df.sh' 20 skip 1 21 fields terminated by 22 whitespace ldrtrim 23 ) 24 location 25 ( 26 exec_dir:'run_df.sh' 27 ) 28 ) 29 / Table created.
38
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 38 External Tables SQL> select * from df; FSNAME BLOCKS USED AVAIL CAPACITY MOUNT ——————————————————————————————— ———————— ———————— ——————— ——————— —————— /dev/mapper/VolGroup00-LogVol00 18156292 10827600 6391528 63% / /dev/sda1 101086 12062 83805 13% /boot tmpfs 517520 0 517520 0% /dev/shm
39
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 39 External Tables – find files on filesystems with less than 20% growth available with fs_data as (select /*+ materialize */ * from df) select mount, file_name, bytes, tot_bytes, avail_bytes, case when 0.2 * tot_bytes < avail_bytes then 'OK' else 'Short on disk space' end status from ( select file_name, mount, avail_bytes, bytes, sum(bytes) over (partition by mount) tot_bytes from ( select a.file_name, b.mount, b.avail*1024 avail_bytes, a.bytes, row_number() over (partition by a.file_name order by length(b.mount) DESC) rn from dba_data_files a, fs_data b where a.file_name like b.mount || '%' ) where rn = 1 ) order by mount, file_name
40
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 40 #4 Model Clause
41
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 41 Model Clause Introduced in 10g Spreadsheet like construct Procedural in nature
42
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 42 Model Clause Here is a common question: I need to have running totals that do not exceed X (say 65,000). For every running total, I need to get the starting key and ending key and the sum of the records for those keys Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000 St_key end_key total ------ ------- ----- 1001 1003 60500 1004 1004 50000 1005 1006 61000 1007 1008 49000
43
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 43 Pipelined function to the rescue? SQL> create or replace type myScalarType as object 2 ( site number, cnt number, the_group number ) 3 / Type created. SQL> create or replace type myTableType as table of myScalarType 2 / Type created.
44
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 44 Pipelined function to the rescue? create or replace function foo( p_cursor in sys_refcursor, p_threshold in number ) return myTableType pipelined as type array is table of t%rowtype index by binary_integer; l_data array; l_running_total number := 0; l_group number := 1; n number := 100; begin loop fetch p_cursor bulk collect into l_data limit N; for i in 1.. l_data.count loop l_running_total := l_running_total + l_data(i).cnt; if ( l_running_total > p_threshold ) then l_group := l_group + 1; l_running_total := l_data(i).cnt; end if; pipe row( myScalarType ( l_data(i).site, l_data(i).cnt, l_group )); end loop; exit when p_cursor%notfound; end loop; close p_cursor; return; end;
45
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 45 Pipelined function to the rescue? ops$tkyte%ORA12CR1> select * 2 from table( foo( cursor( select site, cnt 3 from t 4 order by site ), 65000 ) ); SITE CNT THE_GROUP ---------- ---------- ---------- 1001 10000 1 1002 20000 1 1003 30500 1 1004 50000 2 1005 25000 3 1006 36000 3 1007 28000 4 1008 21000 4 8 rows selected.
46
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 46 Pipelined function to the rescue? ops$tkyte%ORA12CR1> select min(site), max(site), sum(cnt) 2 from ( 3 select * 4 from TABLE( foo( cursor(select site, cnt 5 from t 6 order by site), 65000 ) ) 7 ) 8 group by the_group 9 order by the_group 10 / MIN(SITE) MAX(SITE) SUM(CNT) ---------- ---------- ---------- 1001 1003 60500 1004 1004 50000 1005 1006 61000 1007 1008 49000
47
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 47 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END))
48
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 48 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
49
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 49 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
50
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 50 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
51
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 51 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
52
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 52 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
53
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 53 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
54
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 54 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
55
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 55 Model Clause ops$tkyte%ORA12CR1> SELECT * from ( 2 SELECT s, e, cnt, sm, rn FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN ---- ---- ----- ----- -- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 Site cnt ----- -------- 1001 10000 1002 20000 1003 30500 1004 50000 1005 25000 1006 36000 1007 28000 1008 21000
56
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 56 Model Clause S E CNT SM RN ---------- ---------- ---------- ---------- ---------- 1001 1001 10000 10000 1 1001 1002 20000 30000 2 1001 1003 30500 60500 3 1004 1004 50000 50000 4 1005 1005 25000 25000 5 1005 1006 36000 61000 6 1007 1007 28000 28000 7 1007 1008 21000 49000 8 8 rows selected.
57
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 57 Model Clause ops$tkyte%ORA12CR1> SELECT s, MAX(e), MAX(sm) FROM ( 2 SELECT s, e, cnt, sm FROM t 3 MODEL DIMENSION BY(row_number() 4 over(order by site) rn) 5 MEASURES(site s, site e, cnt, cnt sm) 6 RULES(sm[rn > 1] = 7 CASE WHEN (sm[cv() - 1] + cnt[cv()]) > 65000 8 OR cnt[cv()] > 65000 9 THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > 65000 14 OR cnt[cv()] > 65000 15 THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) 18 GROUP BY s ORDER BY s;
58
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 58 #5 Row Pattern Matching
59
The Question…. I am not able to find the exact answer to my question. I have records like this: Time Amount 11/22/2003 12:22:01 100 11/22/2003 12:22:03 200 11/22/2003 12:22:04 300 11/22/2003 12:22:45 100 11/22/2003 12:22:46 200 11/22/2003 12:23:12 100 11/22/2003 12:23:12 200 What I need to do is sum the amounts where the time of the records is within 3 seconds of each other. In the case where the data is like this: 11/22/2003 12:22:03 200 11/22/2003 12:22:04 200 11/22/2003 12:22:05 200 11/22/2003 12:22:06 200 11/22/2003 12:22:07 200 11/22/2003 12:22:08 200 11/22/2003 12:22:09 200 There would only be one row with the total for all the rows. (Basically, we are looking for "instances" where we define an instance such that all the records within the instance are no more than three seconds apart. So there can be 1 or many records all of the same instance and the resulting summation would have one summary record per instance.) Would you please point me in the right direction?
60
The Answer Start with first row (thinking iteratively here) – If prior row is within 3 seconds -- same group, continue Abs(lag(x) over (order by x)-x) <= 3 seconds – Else new group, break and get a "new" group id – Need to use analytics on top of analytics Inline views -- very powerful here – Demo006.sql
61
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 61 Set Up ops$tkyte%ORA12CR1> create table t ( x date primary key, y int ); Table created.
62
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 62 Set Up ops$tkyte%ORA12CR1> select x,y 2 from t 3 order by x; X Y -------- ---- 12:22:03 94 12:22:04 96 12:22:05 87 12:22:06 94 12:22:07 55 12:22:08 57 12:22:09 100 12:22:10 97 12:22:11 40 12:22:12 13 12:22:18 54 12:22:19 84 12:22:20 26 12:22:36 47 12:22:37 57 12:22:38 52 12:22:39 11 12:22:40 39 12:22:41 4 12:22:42 86 12:22:43 54 12:22:44 47 12:22:45 97 12:22:46 44 12:22:47 17 25 rows selected.
63
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 63 Set Up ops$tkyte%ORA12CR1> select x, y, 2 lag(x) over (order by x), 3 case 4 when abs(lag(x) over (order by x) - x) > 3/24/60/60 5 then row_number() over (order by x) 6 end rn 7 from t 8 /
64
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 64 Set Up … X Y LAG(X)OV RN -------- ---- -------- --- 12:22:03 94 12:22:04 96 12:22:03 12:22:05 87 12:22:04 12:22:06 94 12:22:05 12:22:07 55 12:22:06 12:22:08 57 12:22:07 12:22:09 100 12:22:08 12:22:10 97 12:22:09 12:22:11 40 12:22:10 12:22:12 13 12:22:11 12:22:18 54 12:22:12 11 12:22:19 84 12:22:18 12:22:20 26 12:22:19 12:22:36 47 12:22:20 14 12:22:37 57 12:22:36 12:22:38 52 12:22:37 12:22:39 11 12:22:38 12:22:40 39 12:22:39 12:22:41 4 12:22:40 12:22:42 86 12:22:41 12:22:43 54 12:22:42 12:22:44 47 12:22:43 12:22:45 97 12:22:44 12:22:46 44 12:22:45 12:22:47 17 12:22:46 25 rows selected.
65
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 65 Set Up ops$tkyte%ORA12CR1> select x, y, 2 max(rn) over (order by x) max_rn 3 from ( 4 select x, y, 5 lag(x) over (order by x), 6 case 7 when abs(lag(x) over (order by x) - x) > 3/24/60/60 8 then row_number() over (order by x) 9 end rn 10 from t 11 ) 12 /
66
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 66 Set Up … X Y MAX_RN -------- ---- ---------- 12:22:03 94 12:22:04 96 12:22:05 87 12:22:06 94 12:22:07 55 12:22:08 57 12:22:09 100 12:22:10 97 12:22:11 40 12:22:12 13 12:22:18 54 11 12:22:19 84 11 12:22:20 26 11 12:22:36 47 14 12:22:37 57 14 12:22:38 52 14 12:22:39 11 14 12:22:40 39 14 12:22:41 4 14 12:22:42 86 14 12:22:43 54 14 12:22:44 47 14 12:22:45 97 14 12:22:46 44 14 12:22:47 17 14 25 rows selected.
67
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 67 Set Up ops$tkyte%ORA12CR1> select min(x), max(x), sum(y) 2 from ( 3 select x, y, 4 max(rn) over (order by x) max_rn 5 from ( 6 select x, y, 7 lag(x) over (order by x), 8 case 9 when abs(lag(x) over (order by x) - x) > 3/24/60/60 10 then row_number() over (order by x) 11 end rn 12 from t 13 ) 14 ) 15 group by max_rn 16 order by 1;
68
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 68 Set Up MIN(X) MAX(X) SUM(Y) -------- -------- ---------- 12:22:03 12:22:12 733 12:22:18 12:22:20 164 12:22:36 12:22:47 555
69
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 69 Row Pattern Matching ops$tkyte%ORA12CR1> select * 2 from t 3 match_recognize 4 ( order by x 5 measures first(x) start_time, 6 last(x) end_time, 7 sum(y) sum_y 8 one row per match 9 after match skip past last row 10 pattern (any_row another_row_within_3_secs*) 11 define 12 another_row_within_3_secs as (x-prev(x)) <= 3/24/60/60 13 );
70
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 70 Analytics vs Pattern Matching call count cpu elapsed disk query current rows ------- ------ -------- ---------- ---------- ---------- ---------- ---------- Parse 1 0.00 0.00 0 0 0 0 Execute 1 0.00 0.00 0 0 0 0 Fetch 10 0.30 0.30 0 248 0 122 ------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 12 0.30 0.31 0 248 0 122 Rows (1st) Rows (avg) Rows (max) Row Source Operation ---------- ---------- ---------- --------------------------------------------------- 122 122 122 SORT ORDER BY (cr=248 pr=0 pw=0 time=307541 us cost=492 122 122 122 HASH GROUP BY (cr=248 pr=0 pw=0 time=307388 us cost=492 87800 87800 87800 VIEW (cr=248 pr=0 pw=0 time=289840 us cost=488 size=30 87800 87800 87800 WINDOW BUFFER (cr=248 pr=0 pw=0 time=272816 us cost=488 87800 87800 87800 VIEW (cr=248 pr=0 pw=0 time=197562 us cost=488 size= 87800 87800 87800 WINDOW SORT (cr=248 pr=0 pw=0 time=142009 us cost=488 87800 87800 87800 TABLE ACCESS FULL T (cr=248 pr=0 pw=0 time=10031 us
71
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 71 Analytics vs Pattern Matching call count cpu elapsed disk query current rows ------- ------ -------- ---------- ---------- ---------- ---------- ---------- Parse 1 0.00 0.00 0 0 0 0 Execute 1 0.00 0.00 0 0 0 0 Fetch 10 0.09 0.10 0 248 0 122 ------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 12 0.09 0.10 0 248 0 122 Rows (1st) Rows (avg) Rows (max) Row Source Operation ---------- ---------- ---------- --------------------------------------------------- 122 122 122 VIEW (cr=248 pr=0 pw=0 time=22821 us cost=488 size=2721800 122 122 122 MATCH RECOGNIZE SORT DETERMINISTIC FINITE AUTOMATON (cr=248 87800 87800 87800 TABLE ACCESS FULL T (cr=248 pr=0 pw=0 time=7351 us cost=69
72
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 72 Row Pattern Matching ops$tkyte@ora12cr1> create table stocks 2 ( symbol varchar2(10), 3 tstamp date, 4 price number, 5 primary key (symbol,tstamp) 6 ) 7 organization index 8 / Table created.
73
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 73 Row Pattern Matching ops$tkyte@ora12cr1> declare 2 l_data sys.odciNumberList := 3 sys.odciNumberList 4 ( 35, 34, 33, 34, 35, 36, 5 37, 36, 35, 34, 35, 36, 37 ); 6 l_cnt number := l_data.count; 7 begin 8 for i in 1.. l_cnt 9 loop 10 insert into stocks 11 ( symbol, tstamp, price ) 12 values 13 ('ORCL', sysdate-l_cnt+i, l_data(i) ); 14 end loop; 15 commit; 16 end; 17 / PL/SQL procedure successfully completed.
74
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 74 Row Pattern Matching ops$tkyte@ora12cr1> select symbol, tstamp, price, 2 rpad('*',price,'*') hist 3 from stocks 4 order by symbol, tstamp; SYMBOL TSTAMP PRICE HIST ---------- --------- ---------- ---------------------------------------- ORCL 01-SEP-12 35 *********************************** ORCL 02-SEP-12 34 ********************************** ORCL 03-SEP-12 33 ********************************* ORCL 04-SEP-12 34 ********************************** ORCL 05-SEP-12 35 *********************************** ORCL 06-SEP-12 36 ************************************ ORCL 07-SEP-12 37 ************************************* ORCL 08-SEP-12 36 ************************************ ORCL 09-SEP-12 35 *********************************** ORCL 10-SEP-12 34 ********************************** ORCL 11-SEP-12 35 *********************************** ORCL 12-SEP-12 36 ************************************ ORCL 13-SEP-12 37 ************************************* 13 rows selected.
75
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 75 Row Pattern Matching ops$tkyte@ora12cr1> SELECT * 2 FROM stocks MATCH_RECOGNIZE 3 ( PARTITION BY symbol 4 ORDER BY tstamp 5 MEASURES 6 STRT.tstamp AS start_tstamp, 7 LAST(DOWN.tstamp) AS bottom_tstamp, 8 LAST(UP.tstamp) AS end_tstamp 9 ONE ROW PER MATCH 10 AFTER MATCH SKIP TO LAST UP 11 PATTERN (STRT DOWN+ UP+) 12 DEFINE 13 DOWN AS DOWN.price < PREV(DOWN.price), 14 UP AS UP.price > PREV(UP.price) 15 ) MR 16 ORDER BY MR.symbol, MR.start_tstamp; SYMBOL START_TST BOTTOM_TS END_TSTAM ---------- --------- --------- --------- ORCL 01-SEP-12 03-SEP-12 07-SEP-12 ORCL 07-SEP-12 10-SEP-12 13-SEP-12 SYMBOL TSTAMP PRICE HIST ---------- --------- ---------- ---------------------------------------- ORCL 01-SEP-12 35 *********************************** ORCL 02-SEP-12 34 ********************************** ORCL 03-SEP-12 33 ********************************* ORCL 04-SEP-12 34 ********************************** ORCL 05-SEP-12 35 *********************************** ORCL 06-SEP-12 36 ************************************ ORCL 07-SEP-12 37 ************************************* ORCL 08-SEP-12 36 ************************************ ORCL 09-SEP-12 35 *********************************** ORCL 10-SEP-12 34 ********************************** ORCL 11-SEP-12 35 *********************************** ORCL 12-SEP-12 36 ************************************ ORCL 13-SEP-12 37 ************************************* 13 rows selected.
76
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 of the corporate presentation template 76 Graphic Section Divider
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.