简体   繁体   中英

Why SQL “sum” returns a false value

I have 3 tables and I want to return the customers name and total sum of each deposit and credit amount.

deposit        customers     credit
id             id            id
d_amount                     c_amount
customer_id    name          customer_id
                             type(credit,etc)

I'm doing it by this query =>

SELECT  customers.name ,
sum(deposit.d_amount) as total_depot,
sum(credit.c_amount) as total_credit
from customers
inner join  deposit on deposit.customer_id  = customers.id
inner join  credit on credit.customer_id  = customers.id
and credit.type='credit'
group by customers.id order by customers.name asc

Unfortunately the result of total_depot and total_credit is not correct but when I'm doing it separately like this =>

SELECT  customers.name , sum(deposit.d_amount) as total_depot
from customers
inner join  deposit on deposit.customer_id  = customers.id
group by customers.id order by customers.name asc


SELECT  customers.name , sum(credit.d_amount) as total_credit
from customers
inner join  credit on credit.customer_id  = customers.id
and credit.type='credit'
group by customers.id order by customers.name asc

The result of total_depot and total_credit is correct I don't know where is the error.

The first query is totally wrong, JOINS will multiply lines in result. Example for ONE customer, 3 his credits and 5 his deposits:

select from customers returns 1 line

customers INNER JOIN credits returns 3 lines

customers INNER JOIN credits INNER JOIN deposits returns 15 lines

That is not what you want. Execute your query without SUM and GROUP BY, you will see it.

This is what you want (simplified, not tested):

select customers.id, cr.amount, dep.amount
  from customers
  left join (select customer_id, sum(credit.d_amount) as amount from credits group by customer_id) cr on cr.customer_id=customers.id
  left join (select customer_id, sum(deposits.d_amount) as amount from deposits group by customer_id) dep on dep.customer_id=customers.id

btw. LEFT join is needed for cases when customer does not have BOTH deposit and credit

Do the aggregation before joining the tables:

select c.name, d.total_deposit, cr.total_credit
from customers c join
     (select d.customer_id, sum(d.d_amount) as total_deposit
      from deposit d
      group by d.customer_id
     ) d
     on d.customer_id = c.id join
     (select c.customer_id, sum(c.c_amount) as total_credit
      from credit c
      where c.type = 'credit'
      group by c.customer_id
     ) cr
     on cr.customer_id = c.id
order by c.name asc;

Thanks so lot every body '

i try with all of this exam but it's does not work

i finally make this. its work.

select customers.name,  total_depot , total_credit
from customers 
left join (select customer_id , d_amount , sum(deposit.d_amount) as total_deposit from deposit group by customer_id)  d on d.customers_id = customers.id 
left join (select customer_id , c_amount , sum(credit.c_amount) as total_fact from credit where credit.type='credit' group by customer_id)  c on c.id = customers.id 
group by customers.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM