©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.

Slides:



Advertisements
Similar presentations
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part C Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
Advertisements

5.1Database System Concepts - 6 th Edition Chapter 5: Advanced SQL Advanced Aggregation Features OLAP.
Data Warehouses These slides are a modified version of the slides of the book “Database System Concepts” (Chapter 18), 5th Ed., McGraw-Hill, by Silberschatz,
Chapter 11 Group Functions
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Authorization Forms of authorization on parts of the database: Read - allows.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 18: Data Analysis and Mining.
Chapter 18: Data Analysis and Mining Chapter 18: Data Analysis and Mining Decision Support Systems Data Analysis and OLAP Data Warehousing Data.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
1 1 Data Warehousing Decision-Support Systems  Data Analysis  OLAP  Extended aggregation features in SQL –Windowing and ranking  Implementation Techniques.
Chapter 18: Data Analysis and Mining
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 SQL:1999 Advanced Querying Decision-Support Systems Data Warehousing Data.
Copyright: Silberschatz, Korth and Sudarshan 1 OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Chapter 22: Advanced Querying and Information Retrieval Decision-Support.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 18: Data Analysis and Mining.
1. Data Analysis and Mining 2 Decision Support Systems Data Analysis and OLAP Data Warehousing Data Mining.
Computing & Information Sciences Kansas State University Tuesday, 01 May 2007CIS 560: Database System Concepts Lecture 41 of 42 Tuesday, 01 May 2007 William.
Chapter 22: Advanced Querying and Information Retrieval
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 18: Data Analysis and Mining.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Computing & Information Sciences Kansas State University Monday, 26 Nov 2007CIS 560: Database System Concepts Lecture 37 of 42 Monday, 26 November 2007.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Chapter 22: Advanced Querying and Information Retrieval Decision-Support.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
©Silberschatz, Korth and Sudarshan18.1Database System Concepts - 5 th Edition, Aug 26, 2005 Extended Aggregation in SQL:1999 The cube operation computes.
Computing & Information Sciences Kansas State University Wednesday, 12 Nov 2008CIS 560: Database System Concepts Lecture 31 of 42 Wednesday, 12 November.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 18: Data Analysis and Mining.
More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.
Computing & Information Sciences Kansas State University Wednesday, 29 Nov 2006CIS 560: Database System Concepts Lecture 39 of 42 Wednesday, 29 November.
Decision support, data mining & data warehousing.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Decision support, data mining & data warehousing.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module B: Advanced SQL.
Data warehousing, data analysis and OLAP Sunita Sarawagi
Computing & Information Sciences Kansas State University Thursday, 03 May 2007CIS 560: Database System Concepts Lecture 42 of 42 Thursday, 03 May 2007.
Decision support, data mining & data warehousing.
Decision support, data mining & data warehousing.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 1: Introduction.
Data Analysis Decision Support Systems Data Analysis and OLAP Data Warehousing.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation OLAP Queries and SQL:1999.
Data Analysis and OLAP Dr. Ms. Pratibha S. Yalagi Topic Title
Chapter 22: Advanced Querying and Information Retrieval
Lecturer : Dr. Pavle Mogin
Chapter 5: Advanced SQL.
Lecturer : Dr. Pavle Mogin
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Using Window Ranking, Offset, and Aggregate Functions
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Chapter 5: Advanced SQL.
T-SQL Window Function Deep Dive part 1
Chapter 18: Data Analysis and Mining
Oracle8i Analytical SQL Features
Chapter 18: Data Analysis and Mining
Introduction to T-sql Window functionS
Chapter 22: Advanced Querying and Information Retrieval
Chapter 22: Advanced Querying and Information Retrieval
CS240B: Assignment1 Winter 2016.
Data Warehousing Decision-Support Systems
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Slides based on those originally by : Parminder Jeet Kaur
Presentation transcript:

©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates are either very hard or impossible to specify  Data cube  Complex aggregates (median, variance)  binary aggregates (correlation, regression curves)  ranking queries (“assign each student a rank based on the total marks” SQL:1999 OLAP extensions provide a variety of aggregation functions to address above limitations  Supported by several databases, including Oracle and IBM DB2

©Silberschatz, Korth and Sudarshan22.2Database System Concepts 4 th Edition 2 Extended Aggregation in SQL:1999 The cube operation computes union of group by’s on every subset of the specified attributes E.g. consider the query select item-name, color, size, sum(number) from sales group by cube(item-name, color, size) This computes the union of eight different groupings of the sales relation: { (item-name, color, size), (item-name, color), (item-name, size), (color, size), (item-name), (color), (size), ( ) } where ( ) denotes an empty group by list. For each grouping, the result contains the null value for attributes not present in the grouping.

©Silberschatz, Korth and Sudarshan22.3Database System Concepts 4 th Edition 3 Extended Aggregation (Cont.) Relational representation of crosstab that we saw earlier, but with null in place of all, can be computed by select item-name, color, sum(number) from sales group by cube(item-name, color) The function grouping() can be applied on an attribute  Returns 1 if the value is a null value representing all, and returns 0 in all other cases. select item-name, color, size, sum(number), grouping(item-name) as item-name-flag, grouping(color) as color-flag, grouping(size) as size-flag, from sales group by cube(item-name, color, size) Can use the function decode() in the select clause to replace such nulls by a value such as all  E.g. replace item-name in first query by decode( grouping(item-name), 1, ‘all’, item-name)

©Silberschatz, Korth and Sudarshan22.4Database System Concepts 4 th Edition 4 Extended Aggregation (Cont.) The rollup construct generates union on every prefix of specified list of attributes E.g. select item-name, color, size, sum(number) from sales group by rollup(item-name, color, size)  Generates union of four groupings: { (item-name, color, size), (item-name, color), (item-name), ( ) } Rollup can be used to generate aggregates at multiple levels of a hierarchy. E.g., suppose table itemcategory(item-name, category) gives the category of each item. Then select category, item-name, sum(number) from sales, itemcategory where sales.item-name = itemcategory.item-name group by rollup(category, item-name) would give a hierarchical summary by item-name and by category.

©Silberschatz, Korth and Sudarshan22.5Database System Concepts 4 th Edition 5 Extended Aggregation (Cont.) Multiple rollups and cubes can be used in a single group by clause  Each generates set of group by lists, cross product of sets gives overall set of group by lists E.g., select item-name, color, size, sum(number) from sales group by rollup(item-name), rollup(color, size) generates the groupings {item-name, ()} X {(color, size), (color), ()} = { (item-name, color, size), (item-name, color), (item-name), (color, size), (color), ( ) }

©Silberschatz, Korth and Sudarshan22.6Database System Concepts 4 th Edition 6 Ranking Ranking is done in conjunction with an order by specification. Given a relation student-marks(student-id, marks) find the rank of each student. select student-id, rank( ) over (order by marks desc) as s-rank from student-marks An extra order by clause is needed to get them in sorted order select student-id, rank ( ) over (order by marks desc) as s-rank from student-marks order by s-rank Ranking may leave gaps: e.g. if 2 students have the same top mark, both have rank 1, and the next rank is 3  dense_rank does not leave gaps, so next dense rank would be 2

©Silberschatz, Korth and Sudarshan22.7Database System Concepts 4 th Edition 7 Ranking (Cont.) Ranking can be done within partition of the data. “Find the rank of students within each section.” select student-id, section, rank ( ) over (partition by section order by marks desc) as sec-rank from student-marks, student-section where student-marks.student-id = student-section.student-id order by section, sec-rank Multiple rank clauses can occur in a single select clause Ranking is done after applying group by clause/aggregation Exercises:  Find students with top n ranks  Many systems provide special (non-standard) syntax for “top-n” queries  Rank students by sum of their marks in different courses  given relation student-course-marks(student-id, course, marks)

©Silberschatz, Korth and Sudarshan22.8Database System Concepts 4 th Edition 8 Ranking (Cont.) Other ranking functions:  percent_rank (within partition, if partitioning is done)  cume_dist (cumulative distribution)  fraction of tuples with preceding values  row_number (non-deterministic in presence of duplicates) SQL:1999 permits the user to specify nulls first or nulls last select student-id, rank ( ) over (order by marks desc nulls last) as s-rank from student-marks

©Silberschatz, Korth and Sudarshan22.9Database System Concepts 4 th Edition 9 Ranking (Cont.) For a given constant n, the ranking the function ntile(n) takes the tuples in each partition in the specified order, and divides them into n buckets with qual numbers of tuples. For instance, we an sort employees by salary, and use ntile(3) to find which range (bottom third, middle third, or top third) each employee is in, and compute the total salary earned by employees in each range: select threetile, sum(salary) from ( select salary, ntile(3) over (order by salary) as threetile from employee) as s group by threetile

©Silberschatz, Korth and Sudarshan22.10Database System Concepts 4 th Edition 10 Windowing E.g.: “Given sales values for each date, calculate for each date the average of the sales on that day, the previous day, and the next day” Such moving average queries are used to smooth out random variations. In contrast to group by, the same tuple can exist in multiple windows Window specification in SQL:  Ordering of tuples, size of window for each tuple, aggregate function  E.g. given relation sales(date, value) select date, sum(value) over (order by date between rows 1 preceding and 1 following) from sales Examples of other window specifications:  between rows unbounded preceding and current  rows unbounded preceding  range between 10 preceding and current row  All rows with values between current row value –10 to current value  range interval 10 day preceding  Not including current row

©Silberschatz, Korth and Sudarshan22.11Database System Concepts 4 th Edition 11 Windowing (Cont.) Can do windowing within partitions E.g. Given a relation transaction(account-number, date-time, value), where value is positive for a deposit and negative for a withdrawal  “Find total balance of each account after each transaction on the account” select account-number, date-time, sum(value) over (partition by account-number order by date-time rows unbounded preceding) as balance from transaction order by account-number, date-time