title: Sql Joining 3 Tables Query for Loan Total Result
body: <p>I have to get the total of Customer Loan from 3 tables the two tables are given loan to sum and the other subtract the paid amount and the tables have Customer ID in common. so far I can get the result only if the Customer ID exist in all tables but if it doesn't exist in one table I won't get Customer in my result. or I get NULL customer IDs when I anchor to the customer.</p>
  
  <pre><code>SELECT 
    AS1.C_ID AS [Customer ID],
    ISNULL(AS1.OldCustomerLoan, 0) AS [Old Loan],
    ISNULL(AS2.NewGivenLoan, 0) AS [New Given Loan],
    ISNULL(AS3.LoanPaid, 0) AS [PaidLoanAmount],
    (ISNULL(AS1.OldCustomerLoan, 0) + 
    ISNULL(AS2.NewGivenLoan, 0) - 
    ISNULL(AS3.LoanPaid,0) ) AS Total 
  FROM
    Customer C
    LEFT OUTER JOIN
     (SELECT 
         MOC.C_ID,
         SUM(MOC.Quantity) AS OldCustomerLoan 
      FROM 
         Money_On_Customer MOC (NOLOCK)
      GROUP BY 
         MOC.C_ID) AS1 
      ON c.C_Id = AS1.C_Id
     LEFT OUTER JOIN 
  (SELECT 
         NGL.C_ID 
         ,SUM(NGL.G_Take_Loan) AS NewGivenLoan 
      FROM 
         Given_Loan NGL   
      GROUP BY 
         NGL.C_ID) AS2
      ON c.C_Id = AS2.C_Id
  LEFT OUTER JOIN 
     (SELECT 
         GLP.C_ID, SUM(GLP.G_P_Loan) AS LoanPaid 
      FROM 
         Given_Loan_Paid GLP 
      GROUP BY 
         GLP.C_ID ) AS3
  ON c.C_Id = AS3.C_Id
  </code></pre>
  
  <p>Here Is a picture of my two results:</p>
  
  <p><a href="https://i.stack.imgur.com/4dRUU.png" rel="nofollow noreferrer">When I get NULL Customer IDs</a></p>
  
  <p><a href="https://i.stack.imgur.com/zyOaB.png" rel="nofollow noreferrer">When I don't get All the Customers</a></p>