内部联接导致值的多重化

时间:2013-11-27 09:26:04

标签: sql-server tsql

得到这样的查询:

   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

查询的第一部分可以正常工作。在第二部分(联盟之后)

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]

要成倍增加

我真的不知道如何传递它。有什么想法吗?

在2nd中删除与CaseActionHistory连接的行并连接后,值正确。

3 个答案:

答案 0 :(得分:0)

您是否尝试使用Union代替Union all,因为UNION仅从Select s

检索不同的值

答案 1 :(得分:0)

值乘以但可能不同。

Union而不是union只会做一个不同的,如果有不同的值,“duplicates”将保持不变,因为它们不完全相同。

你应该有一行

val,val,val,1为周数 valdiff,valldiff,valldiff,null作为周数。

我真的不知道出了什么问题,你能否展示3行结果以及你不想要或不出错的内容。 在这一刻,因为我理解你的查询,它应该显示总和的星期和没有星期的全球总和。因此,如果datediff(周,CDV.DateImported,PS.SessionDate),0)总是等于0是,你有2行,一行为0,另一行为null

而不是null使用0并使用它可以为您提供所需的解决方案。

答案 2 :(得分:0)

如果有人在任何时候都有同样的问题:

使用子查询而不是内连接解决