我有这样的查询
select
D.Name
,C.ClientId
,C.SetId
,C.CaseId
,C.ClientCaseNumber
,sum(DD.TransferedCapital) as [TransferedCapital]
,sum(PDH.Capital) as [Capital]
,sum(PDH.Other) as [Other]
,sum(PDH.InterestsU) as [InterestsU]
,PH.AccountType
,sum(PDH.Overpayment) as [Overpayment]
,PROV.ProvisionValue
,PH.OpDate
,C.DateImported
FROM CaseDetails as C
join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId
join PaymentCaseHistory as PH on C.CaseDetailsId = PH.CaseDetailsId
join PaymentDocumentHistory as PDH on PH.PaymentId = PDH.PaymentCaseHistoryId
join ClientProvision as PROV on C.ProductId = PROV.ClientProductId
join DocumentDetails as DD on C.CaseDetailsId = DD.CaseDetailsId
WHERE (PH.IsDeleted is NULL or PH.IsDeleted <> 'True') and C.ProductId = PROV.ClientProductId and C.ClientId= '2'
GROUP by D.Name, C.ClientId, C.CaseId, C.SetId, C.ClientCaseNumber, PH.AccountType, PDH.Overpayment, PH.OpDate, C.DateImported, PROV.ProvisionValue
ORDER by PH.OpDate
问题是:在少数结果中(当有超过 1 个与案例相关的文档时)乘以文档数。
例子:
案例有 3 个文档,当用户添加与案例相关的付款时,价值将分布在所有文档上,直到 0。
案例资本为 6100 但结果显示为 18300。已支付的资本应显示为 4372,03 但显示为 13116,09 等。
可能我在“GROUP BY”部分的某个地方犯了一个错误。
查询应该像这样工作:按案例计算总资本、超额付款等,所以即使案例很少有文件也只显示 1 个总和结果。