Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

LEFT JOIN 3 tables with group by and get sum in SQL ?

By M.K. Verma · 4 May 2022

LEFT JOIN 3 tables with group by and get sum in SQL ?

ou could do a double join using a temporary table :

INSERT INTO t_final(customer_id,customer_name,order_amount) SELECT
    c.customer_id,
    c.customer_name,
    SUM(o.order_amount)
FROM      t_customer c
INNER JOIN t_order o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.customer_name;
UPDATE t_final SET pay_amount = (SELECT 
    SUM(p.pay_amount)
FROM t_payment p WHERE t_final.customer_id = p.customer_id GROUP BY t_final.customer_id LIMIT 1);
SELECT * FROM t_final;