title: SQL Query Advance Salary counting with carry forward of monthly balance
body: <p>I am displaying total balance with carry forward of all months by selecting month
  if i have selected Month March and following </p>
  
  <pre>
  emp_id ==== bal_amt ==== advance_sal ==== date ==== basic_salary
  -----------------------------------------------------------------
   1           48000           2000       2019-01-10     50000
   1           46000           2000       2019-01-11     50000
   2           78000           2000       2019-01-11     80000
   2           75000           3000       2019-01-11     80000
   1           49000           1000       2019-02-10     50000
   2           74000           6000       2019-02-11     80000
  </pre>
  
  <p>if i select month February then i want last balance amount of each id to be selected with total balance amount which is less than February</p>
  
  <p>please see the selected row below</p>
  
  <pre>
  emp_id ==== bal_amt ==== advance_sal ==== date ==== basic_salary
  -----------------------------------------------------------------
   1           48000           2000       2019-01-10     50000
   1           46000           2000       2019-01-11     50000 -- select
   2           78000           2000       2019-01-11     80000
   2           75000           3000       2019-01-11     80000 -- select
   1           49000           1000       2019-02-10     50000 -- select
   2           74000           6000       2019-02-11     80000 -- select
  </pre>
  
  <pre><code>SELECT *
  FROM advance_sal x JOIN
    (SELECT empp_id, max(id)
     FROM advance_sal
     WHERE empp_id = 1
       AND MONTH(`ad_date`)&lt;="2"
       AND YEAR(`ad_date`)&lt;="2019" group_by empp_id) y ON y.empp_id = x.empp_id
  AND y.id = x.id
  ORDER BY x.id
  </code></pre>
  
  <p>So total result would be</p>
  
  <pre>
  emp_id ==== bal_amt ==== advance_sal ==== date ==== basic_salary
  ----------------------------------------------------------------
   1           46000           2000       2019-01-11     50000
   2           75000           3000       2019-01-11     80000
   1           49000           1000       2019-02-10     50000
   2           74000           6000       2019-02-11     80000
  
  emp_id ==== total_bal_amount less than February
  ----------------------------------------------------------------
   1           95000         
   2           149000        
   </pre>
  
  <p>any help to write sql query</p>