Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 1
5 of the Best Things to Happen to SQL Thomas Kyte
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 13 4 #1 Analytics
Analytics Ordered Array Semantics in SQL queries Deptno Ename Sal Select deptno,ename,sal from emp over (partition by deptno King 5000 Clark 2450 Miller 1300 Order by sal desc ) Row_number() SCOTT3000 FORD3000 JONES2975 ADAMS1100 SMITH
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"
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_TOTAL MILLER CLARK KING SMITH ADAMS JONES SCOTT FORD JAMES WARD
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 MILLER % CLARK % KING % 20 SMITH % ADAMS % JONES % SCOTT % FORD %
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 MILLER % CLARK % KING % 20 SMITH % ADAMS % JONES % SCOTT % FORD %
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 KING CLARK MILLER SCOTT FORD JONES
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 KING CLARK MILLER SCOTT FORD JONES
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 KING CLARK MILLER SCOTT FORD JONES ADAMS
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_ SMITH JAMES ADAMS ,800 WARD ,950,800 MARTIN ,1100,950,800 MILLER ,1250,1100,950,800 …
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 MATERIALIZED VIEW CLUSTER DIRECTORY 13.9
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…
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide #2 Pipelined Functions
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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;
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates variable x varchar2(15) exec :x := '1,2,3,a,b,c' PL/SQL procedure successfully completed. select * from table(str2tbl(:x)); COLUMN_VALUE a b c 6 rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT SQL_ID ddk1tv9s5pzq5, child number select * from table(str2tbl(:x)) Plan hash value: | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | | | 29 (100)| | | 1 | COLLECTION ITERATOR PICKLER FETCH| STR2TBL | 8168 | | 29 (0)| 00:00:01 |
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT SQL_ID bd2f8rh30z3ww, child number select /*+ cardinality(sq 10) */ * from table(str2tbl(:x)) sq Plan hash value: | 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 | rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates select 10/8168 from dual; 10/ select /*+ opt_estimate(table, sq, scale_rows= ) */ * from table(str2tbl(:x)) sq Plan hash value: | 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 |
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates 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; 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 /
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates associate statistics with functions str2tbl using str2tbl_stats; Statistics associated.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates Select * from table ( str2tbl( :x||'',',') ) Plan hash value: | 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 |
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates select /*+ dynamic_sampling( sq, 2 ) */ * from table( str2tbl(:x,',') ) sq Plan hash value: | 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)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates with sq as (select /*+ materialize */ * from table( str2tbl( :x ) ) ) select * from sq Plan hash value: | 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 | | 29 (0)| 00:00:01 | | 4 | VIEW | | 8168 | 135K| 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6652_20F4CB | 8168 | | 3 (0)| 00:00:01 | rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Cardinality Estimates with sq as (select /*+ materialize */ * from table( str2tbl( :x ) ) ) select * from sq Plan hash value: | 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 | | 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide #3 External Tables
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide External Tables Query flat files Query output of programs ( 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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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 % / /dev/sda % /boot tmpfs % /dev/shm
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide External Tables SQL> select * from df; FSNAME BLOCKS USED AVAIL CAPACITY MOUNT ——————————————————————————————— ———————— ———————— ——————— ——————— —————— /dev/mapper/VolGroup00-LogVol % / /dev/sda % /boot tmpfs % /dev/shm
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide #4 Model Clause
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Model Clause Introduced in 10g Spreadsheet like construct Procedural in nature
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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 St_key end_key total
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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;
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Pipelined function to the rescue? ops$tkyte%ORA12CR1> select * 2 from table( foo( cursor( select site, cnt 3 from t 4 order by site ), ) ); SITE CNT THE_GROUP rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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), ) ) 7 ) 8 group by the_group 9 order by the_group 10 / MIN(SITE) MAX(SITE) SUM(CNT)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END))
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) S E CNT SM RN Site cnt
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Model Clause S E CNT SM RN rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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()]) > OR cnt[cv()] > THEN cnt[cv()] 10 ELSE sm[cv() - 1] + cnt[cv()] 11 END, 12 s[rn > 1] = 13 CASE WHEN(sm[cv() - 1] + cnt[cv()]) > OR cnt[cv()] > THEN s[cv()] 16 ELSE s[cv() - 1] 17 END)) 18 GROUP BY s ORDER BY s;
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide #5 Row Pattern Matching
The Question…. I am not able to find the exact answer to my question. I have records like this: Time Amount 11/22/ :22: /22/ :22: /22/ :22: /22/ :22: /22/ :22: /22/ :23: /22/ :23: 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/ :22: /22/ :22: /22/ :22: /22/ :22: /22/ :22: /22/ :22: /22/ :22: 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?
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
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Set Up ops$tkyte%ORA12CR1> create table t ( x date primary key, y int ); Table created.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Set Up ops$tkyte%ORA12CR1> select x,y 2 from t 3 order by x; X Y :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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 /
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Set Up … X Y LAG(X)OV RN :22: :22: :22:03 12:22: :22:04 12:22: :22:05 12:22: :22:06 12:22: :22:07 12:22: :22:08 12:22: :22:09 12:22: :22:10 12:22: :22:11 12:22: :22: :22: :22:18 12:22: :22:19 12:22: :22: :22: :22:36 12:22: :22:37 12:22: :22:38 12:22: :22:39 12:22: :22:40 12:22: :22:41 12:22: :22:42 12:22: :22:43 12:22: :22:44 12:22: :22:45 12:22: :22:46 25 rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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 /
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Set Up … X Y MAX_RN :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: :22: rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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;
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Set Up MIN(X) MAX(X) SUM(Y) :22:03 12:22: :22:18 12:22: :22:36 12:22:47 555
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide 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 );
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Analytics vs Pattern Matching call count cpu elapsed disk query current rows Parse Execute Fetch total Rows (1st) Rows (avg) Rows (max) Row Source Operation SORT ORDER BY (cr=248 pr=0 pw=0 time= us cost= HASH GROUP BY (cr=248 pr=0 pw=0 time= us cost= VIEW (cr=248 pr=0 pw=0 time= us cost=488 size= WINDOW BUFFER (cr=248 pr=0 pw=0 time= us cost= VIEW (cr=248 pr=0 pw=0 time= us cost=488 size= WINDOW SORT (cr=248 pr=0 pw=0 time= us cost= TABLE ACCESS FULL T (cr=248 pr=0 pw=0 time=10031 us
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Analytics vs Pattern Matching call count cpu elapsed disk query current rows Parse Execute Fetch total Rows (1st) Rows (avg) Rows (max) Row Source Operation VIEW (cr=248 pr=0 pw=0 time=22821 us cost=488 size= MATCH RECOGNIZE SORT DETERMINISTIC FINITE AUTOMATON (cr= TABLE ACCESS FULL T (cr=248 pr=0 pw=0 time=7351 us cost=69
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Row Pattern Matching 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Row Pattern Matching 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.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Row Pattern Matching select symbol, tstamp, price, 2 rpad('*',price,'*') hist 3 from stocks 4 order by symbol, tstamp; SYMBOL TSTAMP PRICE HIST ORCL 01-SEP *********************************** ORCL 02-SEP ********************************** ORCL 03-SEP ********************************* ORCL 04-SEP ********************************** ORCL 05-SEP *********************************** ORCL 06-SEP ************************************ ORCL 07-SEP ************************************* ORCL 08-SEP ************************************ ORCL 09-SEP *********************************** ORCL 10-SEP ********************************** ORCL 11-SEP *********************************** ORCL 12-SEP ************************************ ORCL 13-SEP ************************************* 13 rows selected.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Insert Information Protection Policy Classification from Slide Row Pattern Matching 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 SEP SEP-12 ORCL 07-SEP SEP SEP-12 SYMBOL TSTAMP PRICE HIST ORCL 01-SEP *********************************** ORCL 02-SEP ********************************** ORCL 03-SEP ********************************* ORCL 04-SEP ********************************** ORCL 05-SEP *********************************** ORCL 06-SEP ************************************ ORCL 07-SEP ************************************* ORCL 08-SEP ************************************ ORCL 09-SEP *********************************** ORCL 10-SEP ********************************** ORCL 11-SEP *********************************** ORCL 12-SEP ************************************ ORCL 13-SEP ************************************* 13 rows selected.
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