title: How to add summary rows to income statements in postgresql
body: <p>Income statement table has structure:</p>
  
  <pre><code>sgroup char(30),
  account char(10),
  jan numeric(12,2),
  feb numeric(12,2)
  </code></pre>
  
  <p>and has values:</p>
  
  <pre><code>SGroup   Account  Jan     Feb
  
  Sales   311      100     200
  Sales   312       20      30
  ..
  Other   410     3333      44
  Other   411      333     344
  ...
  </code></pre>
  
  <p>How convert this table to have header and subtotals for each group:</p>
  
  <pre><code>Caption       Jan     Feb
  
  Sales
   311          100     200
   312           20      30
  Sales Total   120     230
  
  Other
   410         3333     44
   411          333    344
  Other total  3666    388
  ...          ...     ...
  Grand Total  ...     ...
  </code></pre>
  
  <p>Caption column should contain group header, account numbers and group total for each group.
  After total there should be empty row.
  After that that there should be next group etc.
  In the end there should be a "Grand Total" row containing the sum of all rows.</p>
  
  <p>Using Postgres 9.1.2 in Debian.<br>
  Mono C# ASP.NET MVC application running in Debian. If it's more reasonable, this conversion can done in MVC controller also.</p>