08 | Grouping Sets and Pivoting Data

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Chapter 11 Group Functions
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer.
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
SQL Tutorials To understand some of the topics please analyze the following tutorials: The following tutorials will help:
OLAP OPERATIONS. OLAP ONLINE ANALYTICAL PROCESSING OLAP provides a user-friendly environment for Interactive data analysis. In the multidimensional model,
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
17 Copyright © Oracle Corporation, All rights reserved. Enhancements to the GROUP BY Clause.
Chapter 2 Basic SQL SELECT Statements
Version 1.0. MCAD MCSD MCPD Enterprise SQL MCTS MCT Software/Web Development Consultant Cryptography/Digital Signature Consultant SQL Server 2005/2008R2/2012.
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
6 Extraction, Transformation, and Loading (ETL) Transformation.
Modern Database Management Chapter 8: Advanced SQL.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
6 Copyright © 2009, Oracle. All rights reserved. Using the Data Transformation Operators.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Copyright© 2014, Sira Yongchareon Department of Computing, Faculty of Creative Industries and Business Lecturer : Dr. Sira Yongchareon ISCG 6425 Data Warehousing.
 After completing this lesson, you should be able to do the following: ◦ Use the ROLLUP operation to produce subtotal values ◦ Use the CUBE operation.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data.
Module 3: Grouping and Summarizing Data. Summarizing Data by Using Aggregate Functions Summarizing Grouped Data Ranking Grouped Data Creating Crosstab.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Querying with Transact-SQL
How to: SQL By: Sam Loch.
Structured Query Language
Rob Gleasure robgleasure.com
Relational Database Design
Writing Basic SQL SELECT Statements
02 | Advanced SELECT Statements
11 | Error Handling and Transactions
10 | Programming with Transact-SQL
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
SQL/OLAP Sang-Won Lee Let’s e-Wha! URL: Jul. 12th, 2001 SQL/OLAP
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
Structured Query Language – The Basics
05 | Using Functions and Aggregating Data
MySQL Joins MySQL joins are used to combine rows from two or more tables. Different SQL JOINs INNER JOIN: Returns all rows when there is at least one match.
03 | Querying Multiple Tables with Joins
Clothing and Accessories.
06 | Using Subqueries and APPLY
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
02 | Querying Tables with SELECT
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
07 | Using Table Expressions
Data warehouse Design Using Oracle
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Rob Gleasure robgleasure.com
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 1: Basic Queries Aaron Zhi Cheng
02 | Querying Tables with SELECT
Pivoting and Grouping Sets
Presentation transcript:

08 | Grouping Sets and Pivoting Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master

Module Overview Grouping Sets ROLLUP and CUBE Identifying Groupings in Results Pivoting Data Using PIVOT and UNPIVOT

Grouping Sets Syntax GROUPING SETS subclause builds on GROUP BY clause Allows multiple groupings to be defined in same query SELECT <column list with aggregate(s)> FROM <source> GROUP BY GROUPING SETS ( <column_name>,--one or more columns () -- empty parentheses if aggregating all rows );

Grouping Sets Example SELECT EmployeeID, CustomerID, SUM(Amount) AS TotalAmount FROM Sales.SalesOrder GROUP BY GROUPING SETS(EmployeeID, CustomerID,()); EmployeeID CustomerID TotalAmount NULL 256.23 1 49.99 2 107.49 3 98.75 148.74 Total for all sales Subtotals for each customer Subtotals for each employee

ROLLUP and CUBE ROLLUP provides shortcut for defining grouping sets with combinations that assume input columns form a hierarchy CUBE provides shortcut for defining grouping sets in which all possible combinations of grouping sets created SELECT StateProvince, City, COUNT(CustomerID) AS Customers FROM Sales.vCustomerDetails GROUP BY ROLLUP(StateProvince, City) ORDER BY StateProvince, City; SELECT SalesPersonName, CustomerName, SUM(Amount) AS TotalAmount FROM Sales.vSalesOrders GROUP BY CUBE(SalesPersonName, CustomerName) ORDER BY SalesPersonName, CustomerName;

Identifying Groupings in Results Multiple grouping sets present a problem in identifying the source of each row in the result set NULLs could come from the source data or could be a placeholder in the grouping set The GROUPING_ID function provides a method to mark a row with a 1 or 0 to identify which grouping set for the row SELECT GROUPING_ID(SalesPersonName) AS SalesPersonGroup, GROUPING_ID(CustomerName) AS CustomerGroup, SalesPersonName, CustomerName, SUM(Amount) AS TotalAmount FROM Sales.vSalesOrders GROUP BY CUBE(SalesPersonName, CustomerName) ORDER BY SalesPersonName, CustomerName;

Grouping Sets

Pivoting Data Pivoting data is rotating data from a rows-based orientation to a columns-based orientation Distinct values from a single column are projected across as headings for other columns—may include aggregation OrderID ProductID Category Revenue 1023 1 Bikes 1078.75 15 Accessories 52.00 21 124.90 1024 2491.00 1025 3 1067.49 125.99 35 Clothing 26.57 36 5.78 OrderID Bikes Accessories Clothing 1023 1078.75 176.90 NULL 1024 2491.00 1025 1067.49 125.99 32.35 SELECT OrderID, Bikes, Accessories, Clothing FROM (SELECT OrderID, Category, Revenue FROM Sales.SalesDetails) AS sales PIVOT (SUM(Revenue) FOR Category IN([Bikes], [Accessories], [Clothing])) AS pvt

Unpivoting Data Unpivoting data is rotating data from a columns-based orientation to a rows-based orientation Spreads or splits values from one source row into one or more target rows Each source row becomes one or more rows in result set based on number of columns being pivoted OrderID Bikes Accessories Clothing 1023 1078.75 176.90 NULL 1024 2491.00 1025 1067.49 125.99 32.35 OrderID Category Revenue 1023 Bikes 1078.75 Accessories 176.90 1024 2491.00 1025 1067.49 125.99 Clothing 32.35 SELECT OrderID, Category, Revenue FROM (SELECT OrderID, Bikes, Accessories, Clothing FROM Sales.SalesByCat) AS pvt UNPIVOT (Revenue FOR Category IN([Bikes], [Accessories], [Clothing])) AS unpvt

Pivoting and Unpivoting Data

Grouping and Pivoting Data Grouping Sets ROLLUP and CUBE Identifying Groupings in Results Pivoting Data Unpivoting Data Lab: Grouping and Pivoting Data