Reputation: 1
How to check in Oracle Function whether value is increasing in particular field for last 3 year or not. Suppose one table is company and if we want to consider only values if profit of past 3 year of company is in increasing manner.
Upvotes: 0
Views: 399
Reputation: 6866
SELECT empno, sal,
LEAD(sal, 1, 0) OVER (PARTITION BY dept ORDER BY id) NEXT_SAL,
CASE WHEN sal <= LEAD(sal, 1, MAX(SAL)) OVER (PARTITION BY dept ORDER BY sal)
THEN 'OK'
ELSE 'NOT OK'
END STATUS
FROM emp
ORDER BY deptno, sal DESC;
Here if you get all status as 'OK', then value is in incrementing order, else there is some decrement.
LEAD has the ability to compute an expression on the next rows (rows which are going to come after the current row) and return the value to the current row. The general syntax of LEAD is shown below:
LEAD (sql_expr, offset, default) OVER (analytic_clause)
Upvotes: 1