Hassan_Jaffrani
Hassan_Jaffrani

Reputation: 55

sql getting data three columns from one table and one from another

I have two tables sales and customers from 1st table I want to sum two columns recived_amount and bill_amount and subtract them both and after that also want to get customer_id

from 2nd table I just want to get Customer_name

I've tried

  SELECT Customer_details.CUS_name, 
       SUM(SALES.Bill_Amount) - SUM(SALES.Recived_Amount),
       Customer_details.Cus_id
  from sales 
  INNER JOIN Customer_details 
       ON Customer_details.Cus_id=sales.Cus_id
  where SALES.Cus_id = 1 order BY Cus_id

Upvotes: 0

Views: 109

Answers (2)

tigeravatar
tigeravatar

Reputation: 26640

SELECT Customer_details.CUS_name,
       (SUM(SALES.Bill_Amount)-SUM(SALES.Recived_Amount)) AS Balance_Owed,
       Customer_details.Cus_ID
FROM Sales INNER JOIN Customer_details ON Customer_details.Cus_id=Sales.Cus_id
GROUP BY Customer_details.CUS_name,Customer_details.Cus_id
ORDER BY Customer_details.Cus_id;

Upvotes: 0

Ahmed Alaa El-Din
Ahmed Alaa El-Din

Reputation: 1833

 SELECT Customer_details.CUS_name, (SUM(SALES.Bill_Amount) - SUM(SALES.Recived_Amount)) as       Subtract,Customer_details.Cus_id

 from sales INNER JOIN Customer_details ON Customer_details.Cus_id=sales.Cus_id

 where SALES.Cus_id = 1 

 group by Customer_details.CUS_name,Customer_details.Cus_id

 order BY Cus_id

Upvotes: 1

Related Questions