I have a parent table: Deposits (Depid1, Date) and a Child table Depositsdetails (FormOfPmt, Amount) that stores the various payments and form of payments received for each day. The form of payment can be cash, check, DirDepo. I would like to aggregate for each parent record the sum of cash, check, DirDepo. I am currently solving this with with:
------------
SELECT deposits.Depid1, deposits.DepDate, sum(if(depodetails.Pmttype='cash', depodetails.AmtDeposited,0)) as TotCash, sum(if(depodetails.Pmttype='check', depodetails.AmtDeposited,0)) as TotCheck, sum(if(depodetails.Pmttype='DirDepo', depodetails.AmtDeposited,0)) as TotDirDepo
JOIN depodetails depodetails ON deposits.Depid1 = depodetails.Depid2 GROUP BY deposits.Depid1
------------
I get the amounts I need. However I am not sure it's the best approach. I would appreciate any comment/advice if there is a better approach. Thanks
------------
SELECT deposits.Depid1, deposits.DepDate, sum(if(depodetails.Pmttype='cash', depodetails.AmtDeposited,0)) as TotCash, sum(if(depodetails.Pmttype='check', depodetails.AmtDeposited,0)) as TotCheck, sum(if(depodetails.Pmttype='DirDepo', depodetails.AmtDeposited,0)) as TotDirDepo
JOIN depodetails depodetails ON deposits.Depid1 = depodetails.Depid2 GROUP BY deposits.Depid1
------------
I get the amounts I need. However I am not sure it's the best approach. I would appreciate any comment/advice if there is a better approach. Thanks