Skip to main content

Oracle Analytical Functions : Tutorial Part 2 Covers sum avg lag lead.

In this tutorial video we have demonstrated how to use oracle analytical functions like lag , lead , sum and avg etc.

Example 1.
Use of oracle function sum to display running totals with the use of unbounded preceding.
oracle analytical function sum

select sum(Salary) over (order by salary rows unbounded preceding) running_total 
, salary , e.*   from employees e

Identify gap in the contiguous sequences with the lag function.
Ex. In Employees table we have contiguous sequence of employees but due to some anomaly we found that there is now gap in the sequences. Ex Employee Id 210 comes after 206 which is not contiguous.

We can write a query as given below to identify such sequence gaps.
Oracle analytical function Lag
Oracle Lag Function

 select  *  from ( select lag (e.employee_id , 1) over (order by e.employee_id) as prev_emp,   e.* from employees e ) tmp
where (tmp.employee_id - tmp.prev_emp) > 1

Comments

Popular posts from this blog

Introduction to Oracle Analytical functions , rank , denserank.

In this video we tried to demonstrate how oracle anaytical function works. How to do Top N query with oracle's rank and dense_rank function. Also explained is how outputs and functionality of rank and dense_rank differs , what should be used when. The demonstration video is from Linux Mint OS and I have used my own Query Light application. Hope you find it enlightening.   Here are some of the screen prints.  Use of Oracle Rank Analytical Function. Oracle Top N Query using rank analytical functions.