tags:

views:

105

answers:

0

hi to all

I want to get the sum of "debitval" from cdb_php_debit table in which (Cdb_php_debit.rec_id = Cdb_php_dc_rec.id and Cdb_php_dc_rec.id = cdb_php.id) as well as the sum of "creditval" from cdb_php_credit table in which (Cdb_php_credit.rec_id = Cdb_php_dc_rec.id and Cdb_php_dc_rec.id = cdb_php.id) and finally, cdb_php_debit.act_code = cdb_php_credit.cashcode. Please refer to the table below.

I use the code below but incorrect result, because i don't know how to match cdb_php_debit.act_code = cdb_php_credit.cashcode

Thanks in advance

   select 
      cdb_php.id,cdb_php.payee,sum(cdb_php_debit.debitval) as debitval',sum(cdb_php_credit.creditval) as 'creditval' 
   from 
   cdb_php 
   left outer join 
   cdb_php_dc_rec 
   on 
   cdb_php.id = cdb_php_dc_rec.info_id 
   left outer join 
   cdb_php_debit 
   on 
   cdb_php_dc_rec.id = cdb_php_debit.rec_id 
   left outer join 
   cdb_php_credit 
   on 
   cdb_php_dc_rec.id = cdb_php_credit.rec_id
   group by 
   cdb_php_dc_rec.info_id


 Cdb_php (main table)
 Id      date_created payee  description
 1      10/11/09  tirso  Korean meal
 2      10/12/09  clem  japanese meal
 3      10/12/09  raph  Korean meal

 Cdb_php_dc_rec ("info_id" relate in main table "id")
 Id     info_id
 1         1
 2      2
 3      2
 4      3

 Cdb_php_debit (rec_id relate in Cdb_php_dc_rec "id")
 Id     rec_id  act_code              debitval
 1      1  MISCELLANEOUS EXPENSE       100.00
 2      2  MISCELLANEOUS EXPENSE       200.00
 3      3  MISCELLANEOUS EXPENSE       500.00
 4      4  RENTAL - STAFF HOUSE       400.00

 Cdb_php_credit (rec_id relate in Cdb_php_dc_rec "id")
 Id     rec_id  cash_code              creditval
 1      1  MISCELLANEOUS EXPENSE       200.00
 2      2  INTEREST EXPENSES       300.00
 3      3  MISCELLANEOUS EXPENSE       400.00
 4      4  RENTAL - STAFF HOUSE       300.00

 Output table should like this
 Id     date_created payee  description  debitval creditval
 1      10/11/09   tirso   Korean meal  100.00  200.00 
 2      10/12/09   clem   japanese meal  700.00  400.00
 3      10/12/09   raph   Korean meal  400.00  0 OR NULL