SQL - AGGREGATE-FUNCTIONS
SQL - AGGREGATE-FUNCTIONS
SELECT
c.ChannelName AS Channel,
f.SalesAmount AS Sales
FROM FactSales AS f INNER JOIN DimChannel AS c ON f.channelKey=c.ChannelKey
--To avoid repeated values, we need to go for Data Aggregation with Aggregate
Fucntions
/*
**************************************************************************************
*********************
Aggregate
Functions
**************************************************************************************
*********************
Aggregate Fucntions are used to aggregate the data with the following functions
SUM(<number>): It is used to summarize the data. It allows you to pass number and
returns number
COUNT(* or <ColumName>): It is used to get count of rows in table / it returns non-
empty values count if you pass column name
COUNT(DISTINCT <ColumnName>): It is used to get distinct count from the table
AVG(<ColumnName>): It is used to get average or arthematic mean of column
MIN(<ColumName>): It is used to get minimum value of a column
MAX(<ColumName>): It is used to get maximum value of a column
GROUP BY <list of columns>: It is used group the data. We can use this along with
Aggregate functions
Note: If there is normal columns along with aggregated columns, then make sure to use
GROUP BY
with normal columns
*/
--Write a SQL Statement to get total number of employees only if they are eligible for
commission
SELECT
COUNT(Commission) AS ECnt
FROM employee
--Write a SQL statement to get total salary, total number of employees and their
average salary
SELECT
SUM(Salary) AS Salary,
COUNT(*) AS ECnt,
SUM(Salary)/COUNT(*) AS AverageSalary,
AVG(Salary) AS AvgSalary
FROM employee
USE ContosoRetailDW
GO
--Writing SELECT in place of tableName