Search code examples
sqloraclewindow-functions

How to replace NULL in the last row with the same column's value on which Lead() function is applied in Oracle?


I have applied lead() function on Sales Column and since lead() returns NULL for the last row for the Column following_year_sales , I want the value to be 1990776.95 instead of NULL , how to resolve this ?

enter image description here

I have used this query and Output is shown in the image .

SELECT 
 salesman_id,
 year, 
 sales,
 LEAD(sales) OVER (
 ORDER BY year
 ) following_year_sales
FROM 
 salesman_performance
WHERE
 salesman_id = 55;

Please help .


Solution

  • You can call lead() with three parameters. The second would be the number of rows to look ahead and the third a default value that is used in case there is no such row. If you specify the column again as the default, it uses that row's value

    LEAD(sales, 1, sales) OVER (ORDER BY year) following_year_sales