有這樣的查詢:查詢內連接的原因multilplying值
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(PCH.PaymentValue),0) as [PaymentValue]
,COALESCE(datepart(week,PS.SessionDate),0) as [Week]
from
CaseActionHistory as CA
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
join PaymentCaseHistory as PCH on PCH.ActionArchiveId = CA.CaseActionId
join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(CDV.SetId = @SetId or @SetId IS NULL)
group by
CDV.SetId, CDV.DateImported, CDV.ProductName, datepart(week,PS.SessionDate)
UNION
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(CDV.PaymentValue),0) as [PaymentValue]
,'0'
from
CaseActionHistory as CA
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(CDV.SetId = @SetId or @SetId IS NULL)
group by
CDV.SetId, CDV.DateImported, CDV.ProductName
order by
CDV.SetId
第一部分的工作,因爲它應該。在第二部分(UNION ALL)之後
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
使得內部
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(CDV.PaymentValue),0) as [PaymentValue]
相乘
我真的沒有一個想法如何通過它的值。有任何想法嗎?
在第二行中刪除與CaseActionHistory相連的行並參加本身之後,這些值是正確的。
沒有任何改變。 – glaeran