简体   繁体   中英

how to use group by with inner join

I have three tables customer , bargains and installment .
I want to select name from customer table and sum of total price from bargains table and sum of payment amount from installment table according to each customer and at the end subtract the sum of total price from sum of payment amount.
so the result should be a list that contain name of all customers and their debt.

I try the following command but it return wrong values it looks like they are summed twice or maybe more.

select c.name, b.total_price, i.payment_amount
from customer as c
inner join (select sell_or_buy, sum(total_price) as total_price from     bargains group by C_ID ) as b on (b.sell_or_buy = 'خرید')
inner join (select trade_type, sum(payment_amount) as payment_amount from installment group by C_ID ) as i on (i.trade_type = 'پرداخت')
group by c.C_ID

because I thought query sum the total price for each record of installment table and sum the payment amount for each record of bargains table I try to use select in inner joins to make each of them distinct but it doesn't work so I couldn't even subtract the results.
I also try the following query

select
(select sum(payment_amount) as payment_amount from installment  where trade_type = 'پرداخت'group by C_ID)-
(select sum(total_price) as total_price from bargains where sell_or_buy = 'خرید' group by C_ID)as result

which display and error which say query return more than one row.
when I use only one select from above query the result is OK but when I try to combine the its not working.

There is no need to use subquery to do this, just join the three tables and do aggregation, try following:

select 
    c.name,
    sum(b.total_price) as total_price,
    sum(i.payment_amount) as payment_amount
from customer as c
inner join bargains b on c.c_id = b.c_id and b.sell_or_buy = 'خرید'
inner join installment i on c.c_id = i.c_id and i.trade_type = 'پرداخت'
group by c.c_id   -- ,c.name

and like @Tim Biegeleisen said, if the sql_mode of your mysql db contains ONLY_FULL_GROUP_BY , you should also add c.name in group by clause. and use this query to check sql_mode :

show variables like 'sql_mode';

Your original query seems to be missing the correct join conditions. Try this instead:

select c.name,
       b.total_price,
       i.payment_amount
from customer as c
inner join
(
    select C_ID, sum(total_price) as total_price
    from bargains
    where sell_or_buy = 'خرید'
    group by C_ID
) as b
    on c.C_ID = b.C_ID
inner join
(
    select C_ID, sum(payment_amount) as payment_amount
    from installment
    where trade_type = 'پرداخت'
    group by C_ID
) as i
    on c.C_ID = i.C_ID

If you want to compute the difference between the total price and payment amount, then just use this SELECT :

select c.name,
       b.total_price,
       i.payment_amount,
       b.total_price - i.payment_amount AS diff

Can you give me an actual screen shot of each of the tables with a few entries in them?

You're probably getting duplicate rows because of this line: inner join (select trade_type, sum(payment_amount) as payment_amount from installment group by C_ID )

basically, you probably shouldn't use the sum() function while also selecting values from any other field. Here's why: sum(payment_amount) should return 1 row correct? But right next to it you've got "select trade_type" which probably is going to return multiple rows.

Also, what language is this? MySQL? SQL?

Subquery:

SELECT c.Id AS CustomerId, c.Name AS CustomerName, m.Name AS ManagerName, o.SumAmmount 
FROM Customers AS c
INNER JOIN Managers AS m ON c.ManagerId = m.Id
INNER JOIN 
(SELECT SUM(Amount) AS SumAmmount, o.CustomerId
 FROM Orders AS o
 WHERE Date >= '2017-01-01'
 GROUP BY CustomerId
 HAVING SUM(Amount) >= 10000) AS o ON c.Id = o.CustomerId

No Subquery:

SELECT c.Id AS CustomerId, c.Name AS CustomerName, 
       m.Name AS ManagerName, SUM(o.Amount) AS SumAmmount 
FROM Customers AS c
INNER JOIN Managers AS m ON c.ManagerId = m.Id
INNER JOIN Orders AS o ON c.Id = o.CustomerId
WHERE o.Date >= '2017-01-01'
GROUP BY c.Id, c.Name, m.Name
HAVING SUM(Amount) >= 10000

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