2013-11-27 79 views
0

有這樣的查詢:查詢內連接的原因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相連的行並參加本身之後,這些值是正確的。

回答

0

如果有人有問題隨時相同:

使用子查詢,而不是內部解決加盟

0

你試圖使用Union代替Union all,因爲UNION來自Select小號

+0

沒有任何改變。 – glaeran

0

相乘得到的值僅檢索不同的值,但可能是不同的。

聯盟而不是聯盟所有將只是做一個獨特的,如果有不同的價值,「重複」將保持原因,他們不完全相同。

,你應該有一個線

VAL,VAL,VAL,1爲weeknumber valdiff,valldiff,valldiff,null作爲weeknumber。

我真的不知道什麼是錯誤的,你能顯示3行結果,你不想要或錯誤。 在這一刻,因爲我明白你的查詢,它應該顯示一週的差額和全周總和而不用一週。因此,如果datediff(周,CDV.DateImported,PS.SessionDate),0)總是等於0是的,你有2行,一個爲0,另一個爲空

而不是空使用0並使用組通過它也許可以給你你想要的解決方案。

+0

用當前查詢更新了第一篇文章。由於使用「datediff」並不完全符合客戶的要求。無論如何問題依然存在 – glaeran

相關問題