SQL Learning
SQL Learning
select *,
case
when Salary > LAG(salary,1) over (order by emp_id) then ('Higher than previous
employee')
when Salary < LAG(salary,1) over (order by emp_id) then ('Lower than previous
employee')
when Salary = LAG(salary,1) over (order by emp_id) then ('Equal to previous
employee')
end
as Salary_Comparison
from employee
The aggregate window functions are exactly the same as standard aggregate functions, with
the most popular being AVG(), MAX(), MIN(), SUM() and COUNT(). When used in normal
circumstances, these functions either apply to the entire dataset (e.g. AVG() return the mean
from all the values in a column) or go pair-in-pair with a GROUP BY statement so that the
function is applied to subsets or rows, depending on another variable
The GROUP BY statement can be replaced by using an aggregate window function instead
and specifying the aggregation conditions in the OVER clause instead. The main difference
is that while a standard aggregate function reduces the number of rows to match the
number of categories to which the data are aggregated, the window function does not
change the number of rows, instead assigns the correct value to each row of the dataset,
even if these values are the same.