mysql - How can we Substract SUM(s) of two columns from two tables in Cakephp -


i have 2 tables called salarypayment , salarystaff.

salarystaff table

+-----------------+------------+-------------+ | salarystaffid   | employeeid | totalwage   |     +-----------------+------------+-------------+ |      6          |    5       |    80       |   |      7          |    5       |    100      | |      9          |    5       |    60       |  +-----------------+------------+-------------+ 

salarypayment table

+-----------------+------------+-------------+ | salarypaymentid | employeeid | paidamount  |     +-----------------+------------+-------------+ |      1          |    5       |    550      |   +-----------------+------------+-------------+ 

i need result follows,

+-----------------+-----------------+-----------------+-----------------+ |  employeeid     | totalpaidamount | totalwageamount | pendingamount   | +-----------------+-----------------+-----------------+-----------------+ |      5          |    550          |    240          |     210         | +-----------------+-----------------+-----------------+-----------------+ 

totalpaidamount means sum(salarypayment.paidamount )

totalwageamount means sum(salarystaff.totalwage )

pendingamount means sum(salarystaff.totalwage ) - sum(salarypayment .paidamount )

i've tried following, gets totalpaidamount gets triples actual value 1650 , totalwageamount gets proper value.

$this->salarystaff->find('all',array(                 'fields'=>array('salarypayment.*','salarystaff.*','sum(salarystaff.totalwage) totalwageamount','sum(salarypayment.paidamount) totalpaidamount'),                 'joins'=>array(                     array(                                               'alias'=>'salarypayment',                         'table'=>'salary_payments',                                              'conditions' =>array('salarypayment.employeeid = salarystaff.employeeid')                         )                     ),                 'conditions' =>array('salarystaff.employeeid'=>$employeeid),                 'group' => 'salarystaff.employeeid'                 )); 

for specific problem, better off running 2 find's retrieve 2 values need 1 employee.

to implement single query solution, it's easiest understand how write sql first, figure out cakephp. each table's rows needed "grouped" first, "join"-ed.

select salarystaffgrouped.employeeid      , coalesce(totalpaidamount, 0) totalpaidamount      , totalwageamount      , coalesce(totalpaidamount,0)-totalwageamount pendingamount (select employeeid, sum(totalwage) totalwageamount       salarystaff       group employeeid) salarystaffgrouped left join (select employeeid, sum(paidamount) totalpaidamount       salarypayment) salarypaymentgrouped   on salarystaffgrouped.employeeid = salarypaymentgrouped.employeeid; 

there few ways handle implementation within cakephp. recommend query method ease. create mysql views based on grouped/ summarized tables, build cakephp models based on views.


you need aware list employees found in salarystaff table. if employee exists in salarypayment table (and not salarystaff table), person not listed.


if need filter employeeid, query perform better adding 2 where clauses- 1 each of subqueries , not overall query.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -