简体   繁体   中英

How to use SUM with INNER JOIN from two different tables

I am stuck on a query where i have to show list of manufacturers and the Amount of Loan they took and Amount of Refund they returned. i have generated the query but i want to sum total no: of refunds of a manufacturer and total no: of Loans manufacturer has taken. and the difference , that is LoanTaken - RefundAmount = Remaining Amount. If its possible in the query.

SELECT MM.*, ML.*, MR.*  FROM microfinance_manufacturers AS MM 
                INNER JOIN manufacturer_loans AS ML ON MM.ManufacturerId = Ml.ManufacturerId 
                INNER JOIN manufacturer_refunds AS MR ON MM.ManufacturerId = MR.manufacturerId
                WHERE 1 = 1

ManufacturerId  FirstName  LastName  Gender  Religion  PhoneNumber  EmailAddress  Notes              CustomerAddedDateTime  manufacturerTypeId  manufacturerRoles                                                                                                                                                                                                                                                                                                                                                                                 LoanID  ManufacturerId  LoanAmount  LoanDate    RefundId  manufacturerId  RefundAmount  RefundDate  

         5  Saud       Jibran         0         0  8475983748G  HFDKJFH       VGHJXGVHJD         2015-04-29 14:12:20                    21  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_     6               5  65644343:1:"2015-05-06Em       6               5  77744s_Station2015-05-06:{
         5  Saud       Jibran         0         0  8475983748G  HFDKJFH       VGHJXGVHJD         2015-04-29 14:12:20                    21  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_    13               5  543543;s:1:"2015-05-07Em       6               5  77744s_Station2015-05-06:{
         7  Naveed     Ahmed          0         0  847893       hfkjhfskj     fjksddshkfjdshfkj  2015-04-29 14:22:16                    19  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_    16               7  8798u656:1:"2015-05-07Em       9               7  4354334Station2015-05-07:{
         7  Naveed     Ahmed          0         0  847893       hfkjhfskj     fjksddshkfjdshfkj  2015-04-29 14:22:16                    19  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_    16               7  8798u656:1:"2015-05-07Em      10               7  896789798ation2015-05-07:{
         7  Naveed     Ahmed          0         0  847893       hfkjhfskj     fjksddshkfjdshfkj  2015-04-29 14:22:16                    19  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_    17               7  87987687:1:"2015-05-07Em       9               7  4354334Station2015-05-07:{
         7  Naveed     Ahmed          0         0  847893       hfkjhfskj     fjksddshkfjdshfkj  2015-04-29 14:22:16                    19  O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_    17               7  87987687:1:"2015-05-07Em      10               7  896789798ation2015-05-07:{

I have attached the pic of the output i am getting... tables are manufacturer,loan,refund, manufacutrerId is common in all tables. now in the pic it shows repeated record as the manufacturer took loan 2 times but return amount 1 time.. but it repeats in refund column. Please Help !!!

This query should work:

SELECT ManufacturerId,FirstName,LA,RA, LA - RA as RemainingAmount FROM  (SELECT MM.ManufacturerId,MM. FirstName,SUM(LoanAmount) as LA, SUM(RefundAmount) as RA  FROM microfinance_manufacturers AS MM 
                INNER JOIN manufacturer_loans AS ML ON MM.ManufacturerId = Ml.ManufacturerId 
                INNER JOIN manufacturer_refunds AS MR ON MM.ManufacturerId = MR.manufacturerId
                GROUP BY MM.ManufacturerId) as a

You need to do the aggregations before the join . Otherwise, you end up with a cartesian product that throws off the values:

SELECT MM.*, ML.*, MR.* 
FROM microfinance_manufacturers MM left join
     (select Ml.ManufacturerId, count(*) as numloans, sum(ml.amount) as loanamount
      from manufacturer_loans ML
      group by ManufacturerId
     ) ml
     on MM.ManufacturerId = Ml.ManufacturerId left join
     (select Mr.ManufacturerId, count(*) as numrefunds, sum(ml.amount) as refundamount
      from manufacturer_refunds mr
      group by ManufacturerId
     ) mr
     on MM.ManufacturerId = MR.manufacturerId;

Your question is unclear both on the data layout and on the expected results. But this should be basically what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM