0

我有这样的查询

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 个总和结果。

4

2 回答 2

0

由于您声明每个 CaseDetails 行的多个 DocumentDetails 行会为 Capital 和其他一些列生成不正确的值,因此您可以在子查询中获取大部分数据并在外部查询中加入 DocumentDetails 表以获得正确的总和。通过在执行所有其他总和/分组之后加入 DocumentDetails 表,您可以确保加入到子查询结果集的每个 DocumentDetails 行不会被其他加入表倍增。

select
sum(DD.TransferedCapital) as [TransferedCapital]
,r.Name
,r.ClientId
,r.SetId
,r.CaseId
,r.ClientCaseNumber
,r.[Capital] 
,r.[Other]
,r.[InterestsU]
,r..AccountType
,r.[Overpayment]
,r.ProvisionValue
,r.OpDate
,r.DateImported
from (
    select
    D.Name
    ,C.ClientId
    ,C.SetId
    ,C.CaseId
    ,C.ClientCaseNumber
    ,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
    ,C.CaseDetailsId
    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

    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
     ) r
join DocumentDetails as DD on r.CaseDetailsId = DD.CaseDetailsId
ORDER by r.OpDate
于 2013-08-31T22:58:37.157 回答
0

您可以删除加入 DocumentDetails 并替换

sum(DD.TransferedCapital) as [TransferedCapital]

(select sum(DD.TransferedCapital) from CaseDetails CDD inner join DocumentDetails as DD on DD.CaseDetailsId = CDD.CaseDetailsId where C.CaseID = CDD.CaseID) as [TransferedCapital]

此处的条件where DD.CaseDetailsID = C.CaseDetailsID在此子查询中是不可能的,因为 C.CaseDetailsID 不在分组中。CaseID 是。如果您想要不同级别的总计,请更改此 ID。

于 2013-08-31T10:20:50.073 回答