select pop AS yearmonth, ifnull((select sum(s2.stock_count) AS sum_stock_count from c_stock_month_model2 s2 where s2.pop <= t.pop ),0) AS cum_month_stock_count from c_stock_month_model t order by pop;I tried adding a condition in the subquery
select pop AS yearmonth, ifnull((select sum(s2.stock_count) AS sum_stock_count from c_stock_month_model2 s2 where s2.pop <= t.pop and datediff(s2.pop,t.pop)<810),0) AS cum_month_stock_count from c_stock_month_model t order by pop;but it still includes all the data.