join two columns with same title but in different table

user3764208

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?

potashin

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

SQLFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Join same table twice for count in different columns

How to join two columns to the same table

How to join two values from different columns of the same table in Sqlite3?

Join two table with two different Foreign Key for get same field

join same table columns

adding different columns in same table usin inner join in mysql

SQL- Creating an Inner JOIN for Two Columns inside Same Table

How to select rows based on join on two columns of same table

Inner join table that have to be used two times for different columns

Conditional join to two different tables based on 2 columns in 1 table

Left Join the same table based on two different fields?

Join on two table with same primary key but different data

Join two rows by different ids from a same table

Shortening a LEFT JOIN with two different conditions on same Table

Join Table Twice Each With Two Different Values for Same Field

Join two query in the same table on different month and sum value

Unable to do a JOIN when having the same table with two different names

Looking for same values in two different columns in data.table

MySQL Using Same Foreign key for two different table columns

Two MYSQL statements with different columns names but from same table

How to add belongs_to to the same table in two different columns?

Count from two different columns from same table based on range

How to select matching records in two different columns in same table?

MYSQL join two different columns from two different table as single column

SQL join on columns of same table

SQL: How to create a table when the condition is that the data in two columns from two different rows for the same ID be the same?

Join two queries of same table

Inner join same master table three different columns from three different tables based on which is not null

Sqoop teradata table whith same columns title