Calculate row level using previous month and next monthd data in Hive Query

32 Views Asked by At

I have below data as source source_table:

enter image description here

Output I want is

Output_Table: enter image description here

calculation it takes is (take next available amount - previous amount)/ (date of next available amount - date of previous amount) + previous amount

For 2023-07-31 A = (687334-131234)/(2023-09-30 - 2023-06-30) + 131234 For 2023-08-31 A = (687334-131234)/(2023-09-30 - 2023-06-30) + Outputofabove(316600.83)

I have used below code

SELECT
 d,
 a,
 coalesce(a,
   (
   (LEAD(a,2) OVER (ORDER BY d) -  LAG(a,1) OVER (ORDER BY d))
   / 
   datediff(LEAD(d,2) OVER (ORDER BY d) , LAG(d,1) OVER (ORDER BY d))
   +
   LAG(a,1) OVER (ORDER BY d)
   )
   ) As C
FROM source_table;

If i create each internal expression separately, i am getting value, only when i combine all, i am not getting it.

0

There are 0 best solutions below