Open In App

Compute a Difference(Delta) Between Two Columns on Different Rows in PL/SQL

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Calculating the difference between two columns is useful in data analysis. It can help identify trends in various sectors such as finance, healthcare, and inventory management. It can also be used to analyze day-to-day changes in stock prices, and medical diagnoses, and identify bottleneck products.

In this article, we are going to explore different ways to compute a difference(delta) between two columns on different rows in PL/SQL. We will go through various real-world use cases with some clear and concise examples.

Compute a Difference(Delta) Between Two Columns on Different Rows in PL/SQL

We will use a very basic and easy approach. We will try to make it as simple as possible. We will start our PL/SQL block by defining our cursor. This cursor will store the basic table’s information with its delta. Then in the same cursor, we will use JOIN() to join our table’s current row with the same table’s next row. Through this, we can simply calculate the difference by subtracting the next column from the previous column values.

In our next step, we will move to our main BEGIN block. In this block, we will open our defined cursor and display the corresponding values. At the end, we will close our cursor.

Let’s set up an Environment

To understand how to Compute a Difference(Delta) Between Two Columns on Different Rows in PL/SQL we need a table on which we will perform our approach and examples related to it. Here we will consider a table called ‘Geeksforgeeks’ which contains information such as id, name, and score as Columns.

id name score
1 Vishu 200
2 Aayush 300
3 Neeraj 500
4 Sumit 800
5 Vivek 1200

Examples of Computing Differences Between Columns on Different Rows

In this, we will see various examples related to how to compute a difference between two columns on different rows in PL/SQL.

Example 1: Computing the difference between two scores in different rows without any condition.

As specified earlier, we will use a cursor to store our table’s necessary information such as id, score, and difference. We will join the table with the table itself but with the row with an id+1. Through this, we can easily compute the difference between the two scores. Then, we will enter our main block and open our created cursor and we will display all the information stored in the cursor stored earlier. At the end, we will close our cursor.

Query:

DECLARE 
--defining our cursor and storing the tables information along with is delta value.

CURSOR delta_info IS
SELECT g1.id, g1.score, g1.score - g2.score AS delta
FROM geeksforgeeks g1

-- joining the table geeksfirgeeks with current row with a row with id+1
JOIN geeksforgeeks g2 ON g1.id = g2.id + 1;

-- defining our variables
v_id NUMBER;
v_score NUMBER;
v_delta NUMBER;
BEGIN
-- displaying the column headeres

DBMS_OUTPUT.PUT_LINE('id | score | delta');
DBMS_OUTPUT.PUT_LINE('------------------');

--opening the cursor
OPEN delta_info;
LOOP
FETCH delta_info INTO v_id, v_score, v_delta;
EXIT WHEN delta_info%NOTFOUND;

-- displaying the necessary infromation with delta value
DBMS_OUTPUT.PUT_LINE(v_id||' |'|| v_score || ' | ' || v_delta );
END LOOP;

--closing the cursor
CLOSE delta_info;
END;

Output:

id Score delta
2 300 100
3 500 200
4 800 300
5 1200 400

Explanation: In the above image, as we can see there is no data for id 1. This is because there is no previous value to id 1. So we have nothing to calculate the delta between two scores of different rows. We can also see that all the scores are displayed with their corresponding delta values. As we know that id 1 has a 200 score and id 2 has a 300 score. So its delta difference is 100 and we can see that all the rows have done the same calculation for their delta values. You can refer to the above-displayed output to have a more clear understanding of the output.

Example 2: Computing difference between two scores in different rows with a specified condition (odd id’s).

We will use a similar approach to calculate the delta. The difference is that, in this example, we will specify a condition in the statement. We will calculate the delta for some specific id’s such as odd id’s (say 1,3,5). Lets see the query for more clearer understanding.

Query:

DECLARE 
--defining our cursor and storing the tables information along with is delta value.

CURSOR delta_info IS
SELECT g1.id, g1.score, g1.score - g2.score AS delta
FROM geeksforgeeks g1 ,geeksforgeeks g2 WHERE g1.id = g2.id + 2 and g1.id IN (1,3,5);

-- joining the table geeksfirgeeks with current row with a row with id+1

-- defining our variables
v_id NUMBER;
v_score NUMBER;
v_delta NUMBER;
BEGIN
-- displaying the column headeres

DBMS_OUTPUT.PUT_LINE('id | score | delta');
DBMS_OUTPUT.PUT_LINE('------------------');

--opening the cursor
OPEN delta_info;
LOOP
FETCH delta_info INTO v_id, v_score, v_delta;
EXIT WHEN delta_info%NOTFOUND;

-- displaying the necessary infromation with delta value
DBMS_OUTPUT.PUT_LINE(v_id||' |'|| v_score || ' | ' || v_delta );
END LOOP;

--closing the cursor
CLOSE delta_info;
END;

Output:

id score delta
3 500 300
5 1200 700

Explanation: In the above query, we have incremented the id with +2. This will help us to get the odd id’s only. As we can see in the output table, for id 1 score is 200, and for id 3 score is 500. Their difference is 300. Same for id 5, (1200-500 ) 700 is displayed. We have made a little modification in our query, in lines 5 and 6. You can refer to the output table and query, for more clearer understanding.

Conclusion

Overall, computing a difference(delta ) between two columns on different rows has many advantages and real-life use cases. It is used in various such as finance sectors, medical sectors, and many more. In different sectors, it is very important to have a brief look at the fluctuation of data. Through this query, we can easily calculate this fluctuation.

In this article, we have seen how we can calculate the delta between two columns on different rows with and without condition. PL/SQL, being a procedural extension of SQL provides us the flexibility to write our custom scripts. We have seen various examples with their respective explanations. Now you can write queries related to it and can get the desired result.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads