<?xml version="1.0" encoding="windows-1252"?>
<node id="997494" title="Re^3: SQL Database Table to Perl Script" created="2012-10-05 11:11:07" updated="2012-10-05 11:11:07">
<type id="11">
note</type>
<author id="533863">
roboticus</author>
<data>
<field name="doctext">
&lt;p&gt;[prithviraj]:&lt;/p&gt;
&lt;p&gt;You'll want something like:&lt;/p&gt;
&lt;c&gt;
-- compute the totals for each account:
select AccNum, sum(DB_amt) ttl_DB_amt, sum(CR_amt) ttl_CR_amt
from (
   -- transform the table from (accnum, type, amount) into
   -- (accnum, cr_amt, db_amt)
   select accnum,
          case when type='Credit' then Amount else 0 end CR_amt,
          case when type='Debit'  then Amount else 0 end DB_amt
   from YOUR_TABLE_NAME
   where CHOOSE_WHICH_ACCOUNT(S)_YOU_WANT
)
group by AccNum
&lt;/c&gt;
&lt;p&gt;The inner select converts your table into a simpler view of account number, credit amount and debit amount.  The outer select generates the totals for the simple view. &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; For your other questions, simply make the appropriate view transformations and accumulate the totals as above.&lt;/p&gt;
&lt;p&gt;...[roboticus]&lt;/p&gt;
&lt;p&gt;&lt;i&gt;When your only tool is a hammer, all problems look like your thumb.&lt;/i&gt;&lt;/p&gt;</field>
<field name="root_node">
997371</field>
<field name="parent_node">
997398</field>
</data>
</node>
