0

I want to sum of two fields which are from different table acc to labour_master_id. But when I get results it shows different result.

Here is what I tried

select  labours.labour_master_id, 
    sum(labours.amount_paid), 
    sum(labour_cashcredits.amount_paid) 
FROM labours 
left join labour_cashcredits 
    on labours.labour_master_id=labour_cashcredits.labour_master_id 
group by labours.labour_master_id

It may be mapping twice so it shows wrong result.

Now problem is occurring on sum.

Any idea how to do this?

i am having error in this query..please help

select  l.labour_master_id, l.amount_paid as payable, cc.amount_paid as paid
                FROM
                (
                  select labour_master_id, sum(amount_paid) amount_paid
                  from labours 
                  group by labour_master_id
                ) l
                left join
                (
                  select labour_master_id, sum(amount_paid) amount_paid
                  from labour_cashcredits 
                  group by labour_master_id
                ) cc
                                left join
                (
                  select name,id
                  from labour_masters
                ) lm
                    on l.labour_master_id=cc.labour_master_id 
on l.labour_master_id=lm.labour_masters.id
3
  • why are you using left join here? can you please give data sample? Commented Dec 19, 2012 at 11:37
  • 1
    because in second table may data exits or not Commented Dec 19, 2012 at 11:39
  • sums if data exits in second table Commented Dec 19, 2012 at 11:42

4 Answers 4

1

You can try using subqueries to perform the sum in each table:

select  l.labour_master_id, 
    l.amount_paid, 
    cc.amount_paid
FROM
(
  select labour_master_id, sum(amount_paid) amount_paid
  from labours 
  group by labour_master_id
) l
left join
(
  select labour_master_id, sum(amount_paid) amount_paid
  from labour_cashcredits 
  group by labour_master_id
) cc
    on l.labour_master_id=cc.labour_master_id 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it works for me .. is there any method to doing without sub-queries?
@AmandeepKaur You might be able to alter this to only use a subquery on the second table, but IMO it is sometimes easier to use subqueries.
i also want a labour name form labour masters table in my above query. how to join that table with this sub query???
@AmandeepKaur you should be able to just join to the other table using the labour_master_id to return the name.
0

use this--

      SELECT   labours.labour_master_id, SUM (labours.amount_paid),
     SUM (labour_cashcredits.amount_paid)
  FROM labours,labour_cashcredits
     where labours.labour_master_id = labour_cashcredits.labour_master_id
 GROUP BY labours.labour_master_id

Comments

0

Hope this is what you want,

select  labours.labour_master_id, 
    sum(ifnull(labours.amount_paid,0))+
    sum(ifnull(labour_cashcredits.amount_paid),0) sum
FROM labours 
left join labour_cashcredits 
    on labours.labour_master_id=labour_cashcredits.labour_master_id 
group by labours.labour_master_id

Comments

0

Try using sub-query for the second SUM:

SELECT  labours.labour_master_id, SUM(labours.amount_paid), 
          ( SELECT SUM(amount_paid) FROM labour_cashcredits 
            WHERE labour_master_id = labours.labour_master_id 
            GROUP BY labour_master_id
           ) AS credit_amount_paid
FROM labours
LEFT JOIN labour_cashcredits ON labours.labour_master_id = labour_cashcredits.labour_master_id 
GROUP BY labours.labour_master_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.