Show your support for our national animal! Join the movement to stop India’s tigers from fading away. Save Our Tigers - an Aircel initiative in partnership with WWF India.
Hi friends, How to calculate difference between columns of current and previous/next row? Let's say, you want to monitor how often products are sold from your store. i.e. You want difference between current and previous SELL_DATE then you can write query as follows: SELECT TO_CHAR(SELL_DATE, 'DD-MON-YYYY HH24:MI:SS'), TO_CHAR(LAG(SELL_DATE) OVER (ORDER BY SELL_DATE), 'DD-MON-YYYY HH24:MI:SS') AS SELL_DATE_PREV, (SELL_DATE - LAG(SELL_DATE) OVER (ORDER BY SELL_DATE)) * 24 * 60 * 60 AS DIFF_IN_SECONDS FROM PRODUCT_SALES ORDER BY SELL_DATE DESC; More Info: Oracle - Base : Lag Lead Analytic Functions Regards, Sohil Bhavsar
A blog after many years :-) My best practices for PL/SQL are below: Always use Exception Block within each block, function, procedure Send email to your development team for any error which occurs in Production Give top priority to error email Make sure you will never get any error email :-) Write reusable procedures/functions and SQL (If you use APEX you can use List of values) Avoid using Triggers except audit Make sure you add below 6 columns to important tables for Audit. Created by User Creation Date Updated by User Update Date User Agent IP Address Avoid using Stateful Package i.e. do not use package level variables/constants and instead create a DB table which stores all constants. This will avoid causing "ORA-06508: PL/SQL: could not find program unit being called" if you need to do a minor change in package then this could heavily affect where the application has thousands of users. Thanks to Steven Feuerstein.
Comments
Post a Comment