Analytical Function at Rescue

With Analytical function in oracle, we can leverage very powerful data projections for complex data requirement.
It exhibit great tool to play around group sets with in a window and achieve desired outcome.

Will walk  through one of requirement which application team were finding difficult to implement, but with analytical function it was piece of cake.

let try to understand requirement first.
Below was the sample data set and as part of requirement for a particular City we need to have new column projecting previous Step_Number of that City within particular ID.

dataset

So expected output was similar to below.
For instance if City mentioned is Pune, for new column we would update step_number from city Pune itself.
In case City repeat itself, Step_Number need to get updated as shown below for ID = 2.

req_dataset

For such requirement, if we try to achieve same through PLSQL/SQL without using analytical functions surely it would add to more LOC and ultimately more overhead.

From Analytical function we would be using below.

MAX(CASE WHEN CITY = ‘Pune’
THEN STEP_NUMBER
END)
OVER (PARTITION BY ID ORDER BY STEP_NUMBER ROWS BETWEEN UNBOUNDED
PRECEDING AND 1 PRECEDING) REQ_COLUMN;

With in a window of ID, we will traverse reverse and find where city is Pune and get Max Step Number.

Unknown's avatar

About Deepak Mahto

Database Guy with expertise in database migration,performance and Cloud Adoption.
This entry was posted in Helper SQL and tagged , , . Bookmark the permalink.

1 Response to Analytical Function at Rescue

  1. Pingback: Multiplication of Rows Data and Analytical Functions. | Oracle Insight and Performance concepts

Leave a comment