You are on page 1of 15

Lesson 4: Aggregations

Copyright 2016 RESTRICTED CIRCULATION


Aggregate Functions

Copyright 2016 RESTRICTED CIRCULATION


Objectives

What are Aggregate


Functions

Useful Aggregate Functions

Examples

Copyright 2016 RESTRICTED CIRCULATION 3


Aggregate Functions

ProductID CityID MonthID SalesQuantity SalesValue


1 1 201512 100 8000.00
1 2 201605 33 5640.00
1 6 201511 600 7590.00
1 7 201603 55 3433.00
1 9 201601 659 9432.00

Total Sales Quantity :


SUM(SalesValue) = 8000 + 5640 + 7590 + 3433 + 9432
= 34, 095
Sum aggregate function sums up all values in SalesValue column

Copyright 2016 RESTRICTED CIRCULATION 4


What is Aggregate Function?

Find the total quantities sold through Internet Sales in


the year 2006

Column

Select SUM (OrderQuantity) as TotalQuantity from


FactInternetSales where OrderDateKey/10000 = 2006

SUM is an aggregate function here. It aggregates all the


values in OrderQuantity column to return a single value.
Type of aggregation is defined by the function used, in
this case it is sum

Copyright 2016 RESTRICTED CIRCULATION 5


Useful Aggregate Functions

Function Meaning

SUM() Returns sum of all the values in the column

AVG() Returns average of all the values in the column

MIN() Returns the lowest/minimum of all the values in the column

MAX() Returns the highest/maximum of all the values in the column

COUNT() Returns the count of number of values in the column

Copyright 2016 RESTRICTED CIRCULATION 6


Example – AVG() Function

ProductID CityID MonthID SalesQuantity SalesValue


1 1 201512 100 8000.00
1 2 201605 33 5640.00
1 6 201511 600 7590.00
1 7 201603 55 3433.00
1 9 201601 659 9432.00

Average Sales Value :


AVG(SalesValue) = (8000 + 5640 + 7590 + 3433 + 9432)/5
= 6819

Copyright 2016 RESTRICTED CIRCULATION 7


Example – AVG() Function

Write a query to fetch the average unit price of the


Intenet sales recorded in year 2005 for SalesTerritory 6

Select AVG(UnitPrice) as Average_Price from


FactInternetSales where OrderDateKey/10000 =
2005 and SalesTerritorykey=6

AVG is an aggregate function here. It returns the


average of all the values in UnitPrice column

Copyright 2016 RESTRICTED CIRCULATION 8


Example – MIN() Function

ProductID CityID MonthID SalesQuantity SalesValue


1 1 201512 100 8000.00
1 2 201605 33 5640.00
1 6 201511 600 7590.00
1 7 201603 55 3433.00
1 9 201601 659 9432.00

Minimum Sales Quantity :


MIN(SalesQuantity) = 33

Copyright 2016 RESTRICTED CIRCULATION 9


Example – MIN() Function

Write a query to fetch the minimum of


SalesAmountQuota for year 2006 from FactSalesQuota

Select MIN(SalesAmountQuota) as Min_Quota


from FactSalesQuota where CalendarYear=2006

MIN is an aggregate function here. It returns the


minimum of all the values in SalesAmountQuota
column

Copyright 2016 RESTRICTED CIRCULATION 10


Example – MAX() Function

ProductID CityID MonthID SalesQuantity SalesValue


1 1 201512 100 8000.00
1 2 201605 33 5640.00
1 6 201511 600 7590.00
1 7 201603 55 3433.00
1 9 201601 659 9432.00

Maximum Sales Quantity :


MAX(SalesQuantity) = 659

Copyright 2016 RESTRICTED CIRCULATION 11


Example – MAX() Function

Write a query to fetch the maximum of


SalesAmountQuota for year 2006 from FactSalesQuota

Select MAX(SalesAmountQuota) as Max_Quota


from FactSalesQuota where CalendarYear=2006

MAX is an aggregate function here. It returns the


minimum of all the values in SalesAmountQuota
column

Copyright 2016 RESTRICTED CIRCULATION 12


Example – Count() Function

ProductID CityID MonthID SalesQuantity SalesValue


1 1 201512 100 8000.00
1 2 201605 33 5640.00
1 6 201511 600 7590.00
1 7 201603 55 3433.00
1 9 201601 659 9432.00

Count Records :
Count(*) = 5

Copyright 2016 RESTRICTED CIRCULATION 13


Example – COUNT() Function

Write a query to fetch the count of Silver coloured


products from DimProduct table

Could use wildcard character * to refer to all the


columns or could use the specific column name

Select Count(*) as Silver_Products from


DimProduct where Color = Silver
Count is an aggregate function here. It returns the
number of rows returned by the query

Copyright 2016 RESTRICTED CIRCULATION 14


Thank You

Copyright 2016 RESTRICTED CIRCULATION

You might also like