Analytic Functions : An Oracle Developer’s Best Friend

Slides:



Advertisements
Similar presentations
PL/SQL : Stop making the same performance mistakes
Advertisements

Oracle 10g & 11g for Dev Virtual Columns DML error logging
Structured Query Language (SQL)
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should Be able to do the following: Write SELECT statements to accessWrite.
Alles over groeperen Rollups, Cubes, Grouping Sets en hun interne werking Rob van Wijk 22 september 2014.
Web Programming with PL/SQL Erdogan Dogdu Georgia State University Computer Science Department
Copyright  Oracle Corporation, All rights reserved. 3 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access.
After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and.
Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
Copyright: Silberschatz, Korth and Sudarshan 1 OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Chapter 3 Single-Table Queries
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
From Zero to Hero : Using an assortment of caching techniques to improve performance of SQL containing PL/SQL calls. Tim Hall.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Subqueries.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
Oracle Analytic Functions for IR Analysis and Reporting Mingguang Xu and Denise Gardner Office of Institutional Research University of Georgia.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
V 1.0 DBMAN 3 Group By, Having Cube, Rollup OLTP vs OLAP Data analysis 1.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Background Lots of Demos(That’s it.)
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
DATABASES
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
T-SQL Power! Windows That Open Doors Adam
Data Analysis with SQL Window Functions Adam McDonald IT Architect / Senior SQL Developer Smith Travel
Aggregating Data Using Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Data Analysis with SQL Window Functions
Aggregating Data Using Group Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
(SQL) Aggregating Data Using Group Functions
T-SQL Window Function Deep Dive part 1
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Oracle8i Analytical SQL Features
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
Subqueries Schedule: Timing Topic 25 minutes Lecture
CS240B: Assignment1 Winter 2016.
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Analytic Functions : An Oracle Developer’s Best Friend Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCP Advanced PL/SQL Developer Oracle Database: SQL Certified Expert http://www.oracle-base.com Books Oracle PL/SQL Tuning Oracle Job Scheduling http://www.oracle-base.com

What are Analytic Functions? Analytic Functions, or Windowing Functions, were introduced in Oracle8i. They compute aggregate values based on groups of data. Unlike aggregate functions, they don’t reduce the number of rows returned. Analytic functions are processed after the result set is returned, but before the conventional ORDER BY operation. (average.sql) http://www.oracle-base.com

Analytic Function Syntax There is some variation between individual functions, but the basic syntax is similar. The analytic clause is broken down as follows. http://www.oracle-base.com

Analytic Function Syntax http://www.oracle-base.com

query_partition_clause Divides data into partitions or groups, similar to GROUP BY. The operation of the analytic function is restricted to the boundary imposed by these partition. http://www.oracle-base.com

query_partition_clause EMPNO DEPTNO SAL ---------- ---------- ---------- 7934 10 1300 7782 10 2450 7839 10 5000 7369 20 800 7876 20 1100 7566 20 2975 7788 20 3000 7902 20 3000 7900 30 950 7654 30 1250 7521 30 1250 7844 30 1500 7499 30 1600 7698 30 2850 http://www.oracle-base.com

query_partition_clause Divides data into partitions or groups, similar to GROUP BY. The operation of the analytic function is restricted to the boundary imposed by these partition. If the query_partition_clause is empty, the partition is assumed to be the whole result set. (query_partition_clause.sql) http://www.oracle-base.com

order_by_clause Orders rows, or siblings, within a partition. http://www.oracle-base.com

order_by_clause EMPNO DEPTNO SAL ---------- ---------- ---------- 7934 10 1300 7782 10 2450 7839 10 5000 7369 20 800 7876 20 1100 7566 20 2975 7788 20 3000 7902 20 3000 7900 30 950 7654 30 1250 7521 30 1250 7844 30 1500 7499 30 1600 7698 30 2850 1 2 http://www.oracle-base.com

order_by_clause Orders rows, or siblings, within a partition. Necessary with order-sensitive analytic functions. (order_by_clause.sql) Ordering NULLs: ASC is default. When ASC is used, NULLS LAST is default. When DESC is used, NULLS FIRST is default. order_by_clause affects processing order but may not affect display order consistently. MUST use an ORDER BY! http://www.oracle-base.com

windowing_clause Extension of the order_by_clause, giving finer control of the window of operation for some functions. RANGE BETWEEN start_point AND end_point ROWS BETWEEN start_point AND end_point http://www.oracle-base.com

windowing_clause EMPNO DEPTNO SAL ---------- ---------- ---------- 7934 10 1300 7782 10 2450 7839 10 5000 7369 20 800 7876 20 1100 7566 20 2975 7788 20 3000 7902 20 3000 7900 30 950 7654 30 1250 7521 30 1250 7844 30 1500 7499 30 1600 7698 30 2850 http://www.oracle-base.com

windowing_clause Extension of the order_by_clause, giving finer control of the window of operation for some functions. RANGE BETWEEN start_point AND end_point ROWS BETWEEN start_point AND end_point Start and end points. UNBOUNDED PRECEDING : Window starts at first row of partition. UNBOUNDED FOLLOWING : Window ends at last row of partition CURRENT ROW : Window starts or ends at the current row value_expr PRECEDING : Physical/logical offset before current row. value_expr FOLLOWING : Physical/logical offset before current row. Default is “RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW” (windowing_clause.sql) http://www.oracle-base.com

Analytic Functions * = full syntax AVG * CORR * COUNT * COVAR_POP * COVAR_SAMP * CUME_DIST DENSE_RANK FIRST FIRST_VALUE * LAG LAST LAST_VALUE * LEAD LISTAGG MAX * MIN * NTH_VALUE * NTILE PERCENT_RANK PERCENTILE_CONT PERCENTILE_DISC RANK RATIO_TO_REPORT REGR_ (Linear Regression) Functions * ROW_NUMBER STDDEV * STDDEV_POP * STDDEV_SAMP * SUM * VAR_POP * VAR_SAMP * VARIANCE * * = full syntax http://www.oracle-base.com

Analytic Functions : Examples ranking.sql first_last.sql listagg.sql lag_lead.sql first_value.sql last_value.sql row_number.sql http://www.oracle-base.com

Top-N Queries (12c) SELECT empno, sal FROM scott.emp ORDER BY sal DESC Oracle Database 12c finally has a row limiting clause. Makes Top-N Queries and resultset paging easy. How is this implemented? SELECT empno, sal FROM scott.emp ORDER BY sal DESC FETCH FIRST 5 ROWS ONLY; http://www.oracle-base.com

Top-N Queries (12c) http://www.oracle-base.com CONN / AS SYSDBA ALTER SESSION SET EVENTS '10053 trace name context forever, level 1'; SELECT empno, sal FROM scott.emp ORDER BY sal DESC FETCH FIRST 5 ROWS ONLY; ALTER SESSION SET EVENTS '10053 trace name context off’; Final query after transformations:******* UNPARSED QUERY IS ******* SELECT "from$_subquery$_002"."EMPNO" "EMPNO", "from$_subquery$_002"."SAL" "SAL" FROM (SELECT "EMP"."EMPNO" "EMPNO", "EMP"."SAL" "SAL", "EMP"."SAL" "rowlimit_$_ 0", ROW_NUMBER() OVER ( ORDER BY "EMP"."SAL" DESC ) "rowlimit_$$_rownumber" FROM "SCOTT"."EMP" "EMP") "from$_subquery$_002" WHERE "from$_subquery$_002"."rowlimit_$$_rownumber"<=5 ORDER BY "from$_subquery$_002"."rowlimit_$_ 0" DESC http://www.oracle-base.com

Summary Analytic Functions make complex post-query processing simple. Typically much faster than performing similar operations using procedural languages. Very flexible. Takes time to get used to the analytic clause, so keep playing! http://www.oracle-base.com

The End… Slides and Demos: http://www.oracle-base.com/workshops Questions? http://www.oracle-base.com