title: Sum multiple rows while writing a query SQL
body: <p>The query I am using is below:</p>
  
  <pre><code>SELECT
      b.partyID, 
      (select top 1 b.credit) as opening,
      case when (b.total&gt;b.credit) then  b.price else '0' end as debit,
      case when (b.total&lt;b.credit) then b.price else '0' end as credit,
      (select top 1 b.total) as closing
  FROM tblPartyOrder b 
  WHERE b.date&gt;='2014-03-06' AND b.date&lt;='2016-03-09'
  GROUP BY b.partyID, b.credit, b.total, b.price
  ORDER BY b.partyID;
  </code></pre>
  
  <p>Result from query:</p>
  
  <p><a href="https://i.stack.imgur.com/2Tlbj.png" rel="nofollow"><img src="https://i.stack.imgur.com/2Tlbj.png" alt="Result"></a></p>
  
  <p>What I need:</p>
  
  <ol>
  <li>There should be one row for each partyID;</li>
  <li>Keep only the first value as opening (Opening balance as in accounting);</li>
  <li>I want to sum the rows debit credit;</li>
  <li>Keep only last value in closing (closing balance as in accounting).</li>
  </ol>
  
  <p>Please help! 
  Much obliged.</p>