Day 15: Window Functions
1. What are Window Functions?
Window functions perform calculations across a set of rows related to the current row, helping analyze data without grouping.
2. Types of Window Functions
- Aggregate Functions: SUM(), AVG(), COUNT(), MIN(), MAX().
- Ranking Functions: ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE().
- Value Functions: LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE().
Syntax FunctionName() OVER ( PARTITION BY ColumnName ORDER BY ColumnName )
Examples
a) Aggregate with PARTITION
Calculate total salary for each department:
SQL
SELECT EmployeeID, DepartmentID, Salary, SUM(Salary) OVER (PARTITION BY DepartmentID) AS TotalSalary FROM Employees;
In Department 102, with only one employee earning 7000, the total salary is 7000.
b) ROW_NUMBER
Assign a unique number to each row based on salary:
SQL
SELECT EmployeeID, Name, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNumber FROM Employees;
c) RANK and DENSE_RANK
Rank employees based on salary:
SQL
SELECT EmployeeID, Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank, DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank FROM Employees;
RANK: 1, 2, 2 (skips 3 for ties).
DENSE_RANK: 1, 2, 2 (does not skip numbers for ties).
d) LAG and LEAD
Fetch the previous and next salaries in a sequence:
SQL
SELECT EmployeeID, Name, Salary, LAG(Salary) OVER (ORDER BY Salary) AS PreviousSalary, LEAD(Salary) OVER (ORDER BY Salary) AS NextSalary FROM Employees;
PreviousSalary: NULL, 4000, 5000.
NextSalary: 5000, 6000, NULL.
3. Key Takeaways
PARTITION BY groups data into subsets for calculations.
ORDER BY defines the sequence for calculations.
Use window functions to analyze data efficiently without grouping rows.
Action Steps
- Write a query using SUM() and PARTITION BY to calculate group totals.
- Use ROW_NUMBER to rank rows based on any column.
- Experiment with LAG and LEAD to fetch previous and next row values.
No comments:
Post a Comment