Davis Analytical Functions are Cool - New York Oracle...
Transcript of Davis Analytical Functions are Cool - New York Oracle...
![Page 1: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/1.jpg)
Analytical Functions are Cool!
Presented by: Michael DavisCEO OmegaSoft,LLC
![Page 2: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/2.jpg)
Agenda
• What are Analytic Functions• Analytic Function Syntax• Analytic Function Examples
• Simple Example• Top N Ranking Functions• Lag/Lead Functions• Pivot queries
• Question & Answer
![Page 3: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/3.jpg)
What are Analytic Functions
• Extension of ANSI SQL• Major catagories
– Grouping Sets– With Clause– Top n Ranking– Aggregate Window
![Page 4: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/4.jpg)
What are Analytic Functions
• Extension of ANSI SQL (Cont)• Major catagories
– First and Last– Reporting Functions– Lag and Lead– Case and Width_Bucket Functions
![Page 5: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/5.jpg)
Analytic Function syntax
• Function Clause– The function clause begins with a SQL function. A
function statement can include any one of the 33 Analytical Functions, such as SUM, COUNT, ETC.
![Page 6: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/6.jpg)
Analytic Function syntax
• Over Statement– Select MAX() OVER ()– The OVER() statement signals a start of an Analytic
function. That is what differentiates an Analytical Function from a regular Oracle SQL function.
![Page 7: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/7.jpg)
Analytic Function syntax
• Partitioning clause – Select MAX() OVER(partition by field1).
The portioning clause is used to setup the group of data that the Analytic function would be applied to. Though, it’s akin to a Group by statement in a SQL query, it is applied to the result set of a query, and not a group.
![Page 8: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/8.jpg)
Analytic Function syntax
• Order by Clause – Select MAX() OVER(Partition by field order by)
Order by specify the order of the window in the group by statement. The Order by clause is a keyword in the Oracle Analytic syntax that is requirement for using some Analytic functions
![Page 9: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/9.jpg)
Analytic Function syntax
• Window Clause – species the relative rows to which the Analytic
function needs to be applied.
![Page 10: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/10.jpg)
Analytic Function Examples
• Simple ExampleSQL> Select *
1 from ( Select cust_name, sum(clm_amt)clm_amt2 from customer3 group by cust_name 4 order by clm_amt desc desc ) v5 )6 and rownum < 117 /
![Page 11: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/11.jpg)
Analytic Function Examples
• Simple Example resultCUST_NAME CLM_AMT
------------------------------ -----------XYZ 100,000,000Lexus Corp 80,000,000First America 60,000,000Yelp 78,000,000ABC 75,000,000Omega Int. 74,000,000S Corp 70,000,000Acme 25,000,000Sun Enterprise 23,000,000Film studio 17,000,000
![Page 12: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/12.jpg)
Analytic Function Examples
• Analytic Version SQL> select cust_name,
2 SUM(clm_amt) OVER (partition by cust_name) clm_amt3 /
![Page 13: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/13.jpg)
Analytic Function Examples
• Analytic Example resultCUST_NAME CLM_AMT
------------------------------ -----------XYZ 100,000,000Lexus Corp 80,000,000First America 60,000,000Yelp 78,000,000ABC 75,000,000Omega Int. 74,000,000S Corp 70,000,000Acme 25,000,000Sun Enterprise 23,000,000Film studio 17,000,000
![Page 14: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/14.jpg)
Analytic Function Examples
• LEAD/LAG FunctionsSQL> select cust_name,
2 LAG(clm_amt,1,0) OVER (partition by cust_name order by clm_amt) CLM_AMT_PREF
3 LEAD(clm_amt,1,0) OVER (partition by cust_name order by clm_amt) CLM_AMT_NEXT
4 from member5 /
![Page 15: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/15.jpg)
Analytic Function Examples
• LEAD/LAG Function resultCUST_NAME CLM_AMT CLM_AMT_PREV CLM_AMT_NEXT
------------------------------ ----------- ----------- ------------XYZ 100,000,000 0 80,000,000Lexus Corp 80,000,000 100,000,000 60,000,000First America 60,000,000 80,000,000 78,000,000Yelp 78,000,000 60,000,000 75,000,000ABC 75,000,000Omega Int. 74,000,000S Corp 70,000,000Acme 25,000,000Sun Enterprise 23,000,000Film studio 17,000,000 0
![Page 16: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/16.jpg)
Analytic Function Examples
• Top N ranking FunctionSQL> select cust_name, clm_amt,
2 dense_rank() OVER (partition by clm_amt) dr3 from ( select cust_name,4 SUM(clm_amt) OVER (partition by cust_name) clm_amt5 ) 6 /
![Page 17: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/17.jpg)
Analytic Function Examples
• Top N ranking resultCUST_NAME CLM_AMT DR
------------------------------ ----------- --XYZ 100,000,000 1Lexus Corp 80,000,000 2First America 60,000,000 3Yelp 78,000,000 4ABC 75,000,000 5Omega Int. 74,000,000 6S Corp 70,000,000 7Acme 25,000,000 8Sun Enterprise 23,000,000 9Film studio 17,000,000 10
0
![Page 19: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/19.jpg)
Questions and Answers
• Got Questions ?
![Page 20: Davis Analytical Functions are Cool - New York Oracle …nyoug.org/.../2010/September/Davis_Analytical_Functions_are_Cool.pdfAnalytical Functions are Cool! ... That is what differentiates](https://reader031.fdocuments.net/reader031/viewer/2022030507/5ab56dfd7f8b9a1a048cd3d0/html5/thumbnails/20.jpg)
References
• SS64.com. (1999-2010). Analytic Features: http://ss64.com/ora/syntax-analytic.html
• Tom Kyte (2001). Expert One-on-One Oracle: Wrox Press Ltd.