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

SQL - WINDOW - 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)
5 views

SQL - WINDOW - 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

--Write a SQL Statement to show data as per the following columns

--Channel Brand Sales


USE ContosoRetailDW
SELECT TOP 5 * FROM FactSales
SELECT TOP 5 * FROM DimProduct
SELECT TOP 5 * FROM DimChannel

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

SELECT * FROM INFORMATION_SCHEMA.COLUMNS


WHERE COLUMN_NAME LIKE '%Product%'

--Approach-1
-------------
SELECT p.BrandName AS Brand, c.ChannelName AS Channel, SUM(t.Sales) AS Sales FROM (
SELECT
ChannelKey,
ProductKey,
SUM(SalesAmount) AS Sales
FROM FactSales
GROUP BY
ChannelKey,
ProductKey
) AS t INNER JOIN DimProduct AS p ON t.ProductKey=p.ProductKey
INNER JOIN DimChannel AS c ON t.channelKey=c.ChannelKey
GROUP BY p.BrandName, c.ChannelName

--Approach-2
-------------
;WITH cte1 AS (
SELECT
ChannelKey,
ProductKey,
SUM(SalesAmount) AS Sales
FROM FactSales
GROUP BY
ChannelKey,
ProductKey
)
SELECT p.BrandName AS Brand, c.ChannelName AS Channel, SUM(cte1.Sales) AS Sales FROM
cte1
INNER JOIN DimProduct AS p ON cte1.ProductKey=p.ProductKey
INNER JOIN DimChannel AS c ON cte1.channelKey=c.ChannelKey
GROUP BY p.BrandName, c.ChannelName
/*
**************************************************************************************
*********************
Window
Functions
**************************************************************************************
*********************
ROW_NUMBER(): It is used to generate sequence numbers by partition or without
partition

RANK(): It is used to generate rank. After completion of repeated number, rank() will
take row number.
DENSE_RANK(): It is used to generate rank. It will not take row number, after
completion of repeated number

*/

USE SOURCE
--Feed employee data into employees table

SELECT * INTO employees FROM (


SELECT * FROM employee
UNION ALL
SELECT * FROM employee
WHERE EmployeeNo IN(7369, 7654)
) t

SELECT DISTINCT * FROM employees --Not recommended to use DISTINCT

--Write a SQL statement to generate row numbers


SELECT *, ROW_NUMBER() OVER( ORDER BY EmployeeNo) AS Rn FROM employees

--Write a SQL statement to generate row numbers by partition

--Approach-1
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY DepartmentNo ORDER BY EmployeeNo) AS
Rn
FROM employees
) t

--Approach-2
;WITH cte5 AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY DepartmentNo ORDER BY EmployeeNo) AS
Rn
FROM employees
)
SELECT * FROM cte5

--Write a SQL statement to get only unique records by using SELECT in place of table
name and CTE
--Approach-1
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY EmployeeNo ORDER BY EmployeeNo) AS Rn
FROM employees
) t WHERE Rn=1

--Approach-2
;WITH cte5 AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY EmployeeNo ORDER BY EmployeeNo) AS Rn
FROM employees
)
SELECT * FROM cte5
WHERE Rn=1

--Write a SQL statement to generate row numbers based on department


SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY DepartmentNo ORDER BY EmployeeNo) AS
Rn
FROM employees
) t

--Write a SQL statement to generate rank for Salary by using rank()


SELECT *, RANK() OVER( ORDER BY Salary DESC) AS Rnk FROM employees

--Write a SQL statement to generate rank for Salary by using dense_rank()


SELECT *, DENSE_RANK() OVER( ORDER BY Salary DESC) AS Rnk FROM employees

--Write a SQL statement to get highest salary employee details


--Approach-1
SELECT * FROM (
SELECT *, DENSE_RANK() OVER( ORDER BY Salary DESC) AS Rnk FROM employees
) t WHERE t.Rnk=7

--Approach-2
;WITH cte6 AS (
SELECT *, DENSE_RANK() OVER( ORDER BY Salary DESC) AS Rnk FROM employees
)
SELECT * FROM cte6 WHERE Rnk=1

--Write a SQL statement to get 3rd highest salary employee details


--Approach-1

--Approach-2

You might also like