title: How to get count of installments that customers paid or must paid in sql
body: <p>I want to get the count of installments that customers already paid for each bill and how many installments they should pay either they paid parts of the installments or they didn't. I tried this code but I couldn't get the real result either I get paid installments of bill or wrong counts of paid installments.</p>
  
  <pre><code>SELECT
    sb.op_id ,
    sb.auto_bill_no as sales_bill_no ,
    sb.installmnts_cont ,
    sb.installmnts_cont - COUNT(cupa.installment_no) as installmnts_net ,
    sb.bill_total
  FROM sale_bill as sb
  LEFT OUTER JOIN custmrs_paymnts as cupa ON
    sb.installmnt = 1 AND
    sb.installmnt_completed = 0 --AND sb.op_id = cupa.sales_bill_no
  WHERE cupa.installment = 1
    AND sb.custmr_id = @cstmr_no
    AND cupa.custmr_id =     @cstmr_no
  group by sb.op_id , sb.auto_bill_no , sb.installmnts_cont , sb.bill_total
  </code></pre>
  
  <p>Sample data from sale_bill table :</p>
  
  <pre><code>op_id    auto_bill_no   installmnts_cont installmnt_completed  bill_total
  ---------------------------------------------------------------------------
  55           10               2              true                6000
  56           11               2              false               4000
  </code></pre>
  
  <p>Sample data from custmrs_paymnts table :</p>
  
  <pre><code>money_amt         installment    sales_bill_no    installment_no
  -----------------------------------------------------------------
  3000                  true             55               1
  3000                  true             55               2
  </code></pre>
  
  <p>The desired results are:</p>
  
  <pre><code>sales_bill_no    installmnts_cont   installmnts_net   bill_total
  ---------------------------------------------------------------------
  55                    2                    0             6000
  56                    2                    2             4000
  </code></pre>