如果不应该增加价值

时间:2013-08-31 08:51:58

标签: tsql

我有这样的查询

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 等

可能我在&#34; GROUP BY&#34;的某个地方犯了一个错误。部分。

查询应该像这样工作:按案例总计资本,超额支付等,所以即使案例很少,文件只显示1个和的结果。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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