Basically It should add previous 4 year marks in new column (TotMrksinlast4year) i.e if year is 201706 then it should add mark from 201703. I tried using windows function like below but it does not work for me.

sum( case when yearMnth between yearMnth -4 and yearMnth then Marks else 0 end) over (Partition by ID order by yearMnth desc) as TotMrksinlast4year

Appreciate any help. Do let me know if any other way to do it. Thank you!

I might be wrong, but it seems like we’d need a lot more of the problem example to help you. Like is this Spark usage? Is there a DSL involved? Etc. At least show your imports. Show the setup for that one-liner in other words.