Hi Sumeet ,
Use the below and let me know.
No need of loops. You can do it with below logic using rank function .I checked. Its working. But the column and table names accordingly as you require.
select * from ( select
A."Material",
A."Plant2",
A."year /month",
case when A."Mat Standrd" is null then B."Mat Standrd" else A."Mat Standrd" End as "Mat Standrd"
, RANK() over (partition by A."Material", A."Plant2", A."year /month" order by B."year /month" desc) as rank
from "MYDB"."MATERIAL-PLANT-STANDPRICE" A
left join "MYDB"."MATERIAL-PLANT-STANDPRICE" B
on A."Material"=B."Material"
and A."Plant2"=B."Plant2"
and A."year /month" > B."year /month"
and B."Mat Standrd" is not null
) A
where A.rank=1;
Plz check and let me know.
Thanks
Kala