Sie sind auf Seite 1von 14

Lab Session # 6: Aggregate Functions

Introduction

Aggregate functions perform a calculation on a set of values and return a single value. With the
exception of COUNT, aggregate functions ignore null values. Aggregate functions are often used
with the GROUP BY clause of the SELECT statement.
Function Description
Returns the average of the values in a group. Null values are
ignored.
ALL Applies the aggregate function to all values. ALL is the
AVG ( [ ALL | DISTINCT] default.
expression ) DISTINCT Specifies that AVG be performed only on each unique
instance of a value, regardless of how many times the value
occurs.

SUM ( [ ALL | DISTINCT ] Returns the sum of all the values, or only the DISTINCT values, in
expression) the expression . SUM can be used with numeric columns only.
Null ignored.
MAX ( [ ALL | DISTINCT Returns the maximum value in the expression.
] expression)
MIN ( [ ALL | Returns the minimum value in the expression.
DISTINCT]expression)
Returns the number of items in a group. * Specifies that all rows
COUNT ( [ ALL | should be counted to return the total number of rows in a table.
DISTINCT] expression) COUNT(*) takes no parameters and cannot be used with
DISTINCT.
Examples of Aggregate Functions
This example calculates the average advance and the sum of year-to-date sales for all business
books. Each of these aggregate functions produces a single summary value for all of the retrieved
rows.

USE pubs
SELECT AVG(advance), SUM(ytd_sales) FROM titles
WHERE type='business'

SELECT AVG(advance), SUM(ytd_sales) FROM titles


WHERE type='business'

This statement returns the average price of distinct business books.


USE pubs
SELECT AVG(DISTINCT price) FROM titles
WHERE type = 'business'
SELECT AVG(DISTINCT price) FROM titles
WHERE type = 'business'
select * from titles
Without DISTINCT, the AVG function finds the average price of all business titles in the titles table.
USE pubs
SELECT AVG(price) FROM titles WHERE type
= 'business'

The example below shows SUM function giving summary data only.
USE PUBS
SELECT TYPE, SUM(PRICE), SUM(ADVANCE) FROM TITLES
WHERE TYPE LIKE '%COOK'
GROUP BY TYPE
This example returns the book with the highest (maximum) year-to-date sales.

USE pubs
SELECT MAX(ytd_sales) FROM titles

This example returns the book with the lowest (minimum) year-to-date sales.
USE PUBS
SELECT MIN(YTD_SALES)
FROM TITLES

This example finds the number of different cities in which authors live.
USE PUBS
SELECT COUNT(DISTINCT CITY) FROM AUTHORS
This example finds the total number of books and titles.
USE PUBS
SELECT COUNT(*) FROM TITLES

The example shows that COUNT(*) can be combined with other aggregate functions in the select
list.

SELECT COUNT(*), AVG(PRICE) FROM TITLES


WHERE ADVANCE > $1000 USE PUBS

Using SUM and AVG functions with a GROUP BY clause

By itself, an aggregate function produces a single summary value for all rows in a column. If you
want to generate summary values for a column, use aggregate functions with the GROUP BY clause.
Use the HAVING clause with the GROUP BY clause to restrict the groups of rows that are returned.

Use the GROUP BY clause on columns or expressions to organize rows into groups and to
summarize those groups. For example, use the GROUP BY clause to determine the quantity of each
product that was ordered for all orders.

When using GROUP BY clause consider the following guidelines:

· SQL Server produces a column of values for each defined group.


· It returns only single rows for each group that you specify: it does not return detail
information.
· All columns in the Group By clause must be included in the select list.
· Do not use GROUP BY on columns that contain multiple null values because null values are
processed as a group.
This example produces summary values for each type of book that include the average advance for
each type of book and the sum of year-to-date sales for each type of book.
USE pubs
SELECT type, AVG(advance), SUM(ytd_sales) FROM titles
GROUP BY type

This example calculates the sum of the prices and advances for each type of book.
USE pubs
SELECT type, SUM(price), SUM(advance) FROM titles
GROUP BY type
Having Clause
Use HAVING clause on columns or expressions to set conditions on the group included in a result
set. The HAVING clause sets condition on the GROUP BY clause in much the same way that the
WHERE clause interacts with the SELECT clause. Consider following guidelines when using HAVING
clause:

· Use HAVING clause only with the GROUP BY clause.


· Multiple conditions should be combined with logical operators.
· You can reference any of the column that appear in the select list.

USE pubs
SELECT type, AVG(advance), SUM(ytd_sales) FROM titles
GROUP BY type
Having AVG(advance) > 1000
Exercise

Use Northwind database for following queries.

1. Display Maximum and Minimum hire date of all the employees. Rename columns to Max
and Min.

2. Display Average, Max, Min and Sum of Unit Price for all the Products.
3. Write a query to count all the Employees.
4. Write a query to count the orders that have been placed by individual customers.
[Hint: Use Group By clause]
5.Each Employee reports to some person. Write a query to display the Reportsto field along
with the number of employees who report to this employee.
Use Pubs database for the following queries.
5. Display the average price for all the books of business type. (Use Titles table)

6. Count all the publishers who live in USA. (Use publishers table) [use group by and having
clause]
1.What is the difference among char, varchar, nchar and nvarchar?

Char

 It is a fixed length data type


 Used to store non-Unicode characters
 Occupiers 1 byte of space for each character

Varchar

 It is a variable length data type


 Used to store non-Unicode characters
 Occupies 1 byte of space for each character

If you are sure about the fixed length of the data that would be captured for any specific
column then go for CHAR data type and if the data may vary then go for VARCHAR.
nChar

 Is a fixed length data type


 Used to store Unicode characters (for example the languages Arabic, German and so on)
 Occupies 2 bytes of space for each character

nvarChar

 It is a variable-length data type


 Used to store Unicode characters
 Occupies 2 bytes of space for each character
If your column will store a fixed-length Unicode characters like French, Arabic and so on
characters then go for NCHAR. If the data stored in a column is Unicode and can vary
in length, then go for NVARCHAR.

2.How can where clause be used with group by clause?

Das könnte Ihnen auch gefallen