Aggregations Various Aggregation Functions GROUP BY HAVING.

Slides:



Advertisements
Similar presentations
Chapter 11 Group Functions
Advertisements

Said Salomon Unitrin Direct Insurance T-SQL Aggregate Functions Said Salomon.
SQL Language. Introduction to RDBMS Introduction to RDBMS Basic Data Manipulation - Reading Data Basic Data Manipulation - Reading Data Basic Data Manipulation.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Introduction to Oracle9i: SQL1 SQL Group Functions.
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
 Continue queries ◦ You completed two tutorials with step-by-step instructions for creating queries in MS Access. ◦ Now must apply knowledge and skills.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
Reports 5.02 Understand database queries, forms, and reports used in business.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Chapter 3 Query and Report. Agenda Report types Report contents Report creation Report design view Query and dynaset Function and grouping Action query.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
IFS180 Intro. to Data Management Chapter 11 - Subqueries.
© Jalal Kawash Database Queries Peeking into Computer Science.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
Structured Query Language SQL Unit 4 Solving Problems with SQL.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
In-Class SQL Query Exercises For the Plumbing Supply Store Database.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
SQL Query Getting to the data ……..
CS3220 Web and Internet Programming More SQL
Structured Query Language
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Queries.
LESSON 19-1 Determining the Quantity of Merchandise Inventory
LESSON 19-1 Determining the Quantity of Merchandise Inventory
The Database Exercises Fall, 2009.
Structured Query Language – The Basics
20761A 10: Using Subqueries Module 10   Using Subqueries.
05 | Using Functions and Aggregating Data
Database Queries.
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
20761B 10: Using Subqueries Module 10   Using Subqueries.
Data warehouse Design Using Oracle
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Access Quiz.
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
Chapter 7 Most important: 7.2
Access: SQL Participation Project
Determining the Quantity of Merchandise Inventory
Action Query Exercises
SQL Aggregation.
Structured Query Language – The Fundamentals
Exploring Microsoft® Office 2016 Series Editor Mary Anne Poatsy
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Query Functions.
Access: Queries III Participation Project
Section 4 - Sorting/Functions
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
LINQ to SQL Part 3.
Chapter 3 Query and Report.
Grouping and Aggregating Data
Group Operations Part IV.
Presentation transcript:

Aggregations Various Aggregation Functions GROUP BY HAVING

Aggregate Functions SQL Provides several functions that act on columns to provide aggregates of the values in the column Find them under the Programmability | Functions | System Functions | Aggregate Functions section of the Object Browser

Aggregate Functions (cont.) Try these two queries (run them together) SELECT Quantity, UnitPrice, Quantity * UnitPrice AS 'Total' FROM [Order Details] WHERE OrderID = 10609 SELECT SUM(Quantity) AS 'Total Units Sold', SUM(Quantity * UnitPrice) AS 'Total Value Sold'

Aggregate Functions (cont.) Aggregate functions return one row with the aggregate values as the columns You must use AS to give a name to the aggregate column as it will not have a 'natural' name Most useful functions AVG ( ) − COUNT( ) MIN ( ) − MAX( ) SUM ( ) Statistical functions STDEV ( ) − STDEVP ( ) VAR ( ) − VARP ( )

Aggregate Functions (cont.) The argument of each aggregate function is usually a column name It may be a calculated expression SUM (Quantity * UnitPrice) AS 'Total Value Sold' The COUNT ( ) function returns the number of rows in the result set regardless of which column is used SELECT COUNT (*) AS… is commonly used When a WHERE clause limits the rows with a criteria the aggregations are performed only on the rows that meet the WHERE criteria Aggregates can be used in multi-table queries

Aggregate Function Exercises Find the last OrderID in the DB Find out what the next ProductID will be without actually adding a product Find out the value of the inventory on hand (based on sales price) of the discontinued products Find out the average number of products sold whenever the product Carnarvon Tigers have been sold

Grouping in Aggregate Functions Without grouping an aggregate query will return just one row SELECT * FROM [Order Details] WHERE OrderID = 10523 SELECT Orders.OrderID, COUNT(*) AS 'Number of Products', SUM(Quantity) AS 'Number of Items', SUM(Quantity * UnitPrice * (1 - Discount)) AS 'Revenue' FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID WHERE DatePart(mm, OrderDate) = 5 AND DatePart(yy, OrderDate) = 1997 GROUP BY Orders.OrderID

GROUP BY GROUP BY in an aggregate query gives one row for each distinct value of the fields in the GROUP BY clause Multiple items in the GROUP BY list can create multiple levels of grouping Add ProductID to the GROUP BY clause of the previous query GROUP BY OrderID, ProductID Items are grouped by the first item, then by the second within the first, etc.

GROUP BY (cont.) Any column in the SELECT clause that is not an aggregate expression must be listed in the GROUP BY expression Run the previous query with the GROUP BY commented out If you wish to display multiple non-aggregated columns then they all must be listed in the GroupBy Rewrite the previous query to display not only the OrderID but also the Customer CompanyName You can apply an ORDER BY clause on an aggregated column when using GROUP BY

GROUP BY Exercises Find the total value of each product in inventory by category (based on selling price) by showing the CategoryID, the quantity on hand, and the total value Show the total value of sales by sales rep during May 1997. Show the sales rep's name and value.

Run this query with and without the last line HAVING HAVING acts like a WHERE clause applied to the results of a GROUP BY aggregation Run this query with and without the last line SELECT Orders.OrderID, COUNT(*) AS 'Number of Products', SUM(Quantity) AS 'Number of Items', SUM(Quantity * UnitPrice * (1 - Discount)) AS 'Revenue' FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID WHERE DatePart(mm, OrderDate) = 5 AND DatePart(yy, OrderDate) = 1997 GROUP BY Orders.OrderID HAVING SUM(Quantity) < 10

HAVING Exercises Management wants to know the names of your best customers. Find the CompanyNames and value purchased for all customers who ordered more than $1,000 in merchandise in May of 1997.