I have a table pur
with values as
CUST_ID AMOUNT
1 100
3 50
1 200
1 500
3 20
and table cus
with values as:
CUST_ID
1
2
3
How do I get to show this:
CUST_ID Total_AMOUNT
2 0
3 70
1 800
Simply put, how do I show 2 columns from cus
table?
You can use LEFT JOIN
and then get SUM
of the AMOUNT
by grouping rows with the same CUST_ID
:
SELECT t2.CUST_ID
, COALESCE(SUM(t1.AMOUNT), 0) AS TOTAL
FROM cus t2 LEFT JOIN pur t1 ON t1.CUST_ID = t2.CUST_ID
GROUP BY t2.CUST_ID
ORDER BY TOTAL
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments