0% found this document useful (0 votes)
10 views

SQL - AGGREGATE-FUNCTIONS

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL - AGGREGATE-FUNCTIONS

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

--ChannelName SalesAmount

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 salary of employees


USE DATASTORE
SELECT
SUM(Salary) AS Salary
FROM employee

--Write a SQL Statement to get total number of employees


SELECT
COUNT(*) AS ECnt
FROM employee

--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 number of jobs


SELECT
COUNT(DISTINCT Job) AS JobCnt
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

--Write a SQL Statement to get minimum Salary amd maximum Salary


SELECT
MIN(Salary) AS MinSalary,
MAX(Salary) AS MaxSalary
FROM employee

--Write a SQL Statement to get minimum Hiredate and maximum Hiredate


SELECT
MIN(Hiredate) AS MinHiredate,
MAX(Hiredate) AS MaxHiredate
FROM employee

--Write a SQL Statement to get total salaries by department number wise


SELECT
DepartmentNo,
SUM(Salary) AS Salary
FROM employee
GROUP BY DepartmentNo

--Write a SQL statement to get total salaries by job


SELECT
Job,
SUM(Salary) AS Salary
FROM employee
GROUP BY Job

--Write a SQL Statement to get total salary by Year


SELECT
YEAR(Hiredate) AS Year,
SUM(Salary) AS Salary
FROM employee
GROUP BY YEAR(Hiredate)

USE ContosoRetailDW

--Writing above statement by writing SELECT in place of tableName


--Syn: SELECT * FROM <tableName>

--Write a SQL statement to get Sales By ChannelKey


SELECT
c.ChannelName AS Channel,
SUM(f.SalesAmount) AS Sales
FROM FactSales AS f INNER JOIN DimChannel AS c ON f.channelKey=c.ChannelKey
GROUP BY c.ChannelName

GO
--Writing SELECT in place of tableName

SELECT c.ChannelName AS Channel, t.Sales FROM (


SELECT
ChannelKey,
SUM(SalesAmount) AS Sales
FROM FactSales
GROUP BY ChannelKey
) AS t INNER JOIN DimChannel AS c ON t.channelKey=c.ChannelKey

--By using CTE's


--CTE-Common Table Expression:
;WITH cte1 AS (
SELECT
ChannelKey,
SUM(SalesAmount) AS Sales
FROM FactSales
GROUP BY ChannelKey
)
SELECT c.ChannelName AS Channel, cte1.Sales FROM cte1 INNER JOIN DimChannel AS c ON
cte1.channelKey=c.ChannelKey

-- By using Temparary table


SELECT
ChannelKey,
SUM(SalesAmount) AS Sales INTO #temptbl
FROM FactSales
GROUP BY ChannelKey

SELECT c.ChannelName AS Channel, t1.Sales FROM #temptbl AS t1 INNER JOIN DimChannel AS


c ON t1.channelKey=c.ChannelKey

--Write a SQL Statement to get Department Name and Total Salary


--DepartmentName Salary
--By using SELECT in place of TableName

You might also like