我被困在一个查询中,我必须显示制造商的清单和他们采取的贷款金额以及他们退回的退款金额。我已经生成了查询,但我想总计没有:制造商的退款和贷款制造商已经采取的总数:。和差异,即LoanTaken - RefundAmount =剩余金额。如果它可以在查询中。
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:{
我附上了我正在获得的输出图片... 表是制造商,贷款,退款,manufacutrerId在所有表中都很常见。 现在在图片中显示重复记录,因为制造商贷款2次但返还金额1次..但它在退款栏中重复。请帮助!!!
答案 0 :(得分:0)
此查询应该有效:
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
答案 1 :(得分:0)
您需要在 join
之前进行聚合。否则,你最终会得到一个叛逆的产品,它会抛出这些值:
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;
您的问题在数据布局和预期结果方面都不清楚。但这基本上应该是你需要的。