2017-01-03 67 views
0

我写过一个连接查询语句。那条语句再次返回多个(重复)行,即使我只有一条记录。使用多个连接重复记录

declare @BenefitClass int ; 

set @BenefitClass = (select BenefitClass From HJOB where userid='d76c5000-69e0-461e-92e1-3cfe7590d098' and CompanyId =1629) 
select @BenefitClass; 
select 
bve.EmployerContribution, 
bhsac.CatchUpValue as CatchUpValue , 
bcl.Tier, 
bcl.planYear, 
bhsac.Ischecked, 
isnull(bhsac.Value,0) as EmployeeContribute, 
Id=(convert(varchar, bcl.Id) + '$' + convert(varchar, isnull(bhsac.Id, 0))) , 
bhsac.Value , 
bhsac.HSALmitId 
from  
dbo.benContributionStructure bcs 
inner join dbo.benVariableElection bve on bcs.PlanInfoId = bve.PlanInfoId 
inner join dbo.benBenefitContributionLimit bcl on bcs.SavingCategory =  bcl.CategoryID 
left outer join dbo.benBenefitHSACoverage bhsac on bcs.PlanInfoId = bhsac.planInfoId 
     and bcl.Id=bhsac.HSALmitId --and [email protected] 
     and bhsac.UserID='d76c5000-69e0-461e-92e1-3cfe7590d098' and bhsac.PlanInfoId=38044 
left outer join dbo.benEmployeeContribution bec on bhsac.UserID = bec.UserId and bhsac.BenefitClassId = bec.BenefitClassId -- and bec.EnrollmentType !='Closed' 
left outer join benOpenEnrollment oems on oems.ID = bec.OpenEnrollmentId and oems.EndDt > GETDATE()    
where  
bcs.PlanInfoId=38044 and bcl.Ischecked=1 
         and bcl.Tier !='CatchUp'  
    and bcl.CompanyId=1629 

对于我得到的结果作为第二排为一式两份:

observe the result

回答

1

试试这一次它可能帮你

declare @BenefitClass int ; 

set @BenefitClass = (select BenefitClass From HJOB where userid='d76c5000-69e0-461e-92e1-3cfe7590d098' and CompanyId =1629) 
select @BenefitClass; 

;with cte as (
select 
bve.EmployerContribution, 
bhsac.CatchUpValue as CatchUpValue , 
bcl.Tier, 
bcl.planYear, 
bhsac.Ischecked, 
isnull(bhsac.Value,0) as EmployeeContribute, 
Id=(convert(varchar, bcl.Id) + '$' + convert(varchar, isnull(bhsac.Id, 0))) , 
bhsac.Value , 
bhsac.HSALmitId 
from  
dbo.benContributionStructure bcs 
inner join dbo.benVariableElection bve on bcs.PlanInfoId = bve.PlanInfoId 
inner join dbo.benBenefitContributionLimit bcl on bcs.SavingCategory =  bcl.CategoryID 
left outer join dbo.benBenefitHSACoverage bhsac on bcs.PlanInfoId = bhsac.planInfoId 
     and bcl.Id=bhsac.HSALmitId --and [email protected] 
     and bhsac.UserID='d76c5000-69e0-461e-92e1-3cfe7590d098' and bhsac.PlanInfoId=38044 
left outer join dbo.benEmployeeContribution bec on bhsac.UserID = bec.UserId and bhsac.BenefitClassId = bec.BenefitClassId -- and bec.EnrollmentType !='Closed' 
left outer join benOpenEnrollment oems on oems.ID = bec.OpenEnrollmentId and oems.EndDt > GETDATE()    
where  
bcs.PlanInfoId=38044 and bcl.Ischecked=1 
         and bcl.Tier !='CatchUp'  
    and bcl.CompanyId=1629 
    ) 
    select distinct EmployerContribution, 
CatchUpValue ,Tier,planYear,Ischecked,EmployeeContribute,Id ,Value ,HSALmitId from cte 
+0

我能知道你为什么和CTE一起去? –

+0

没有什么了不起的技巧..我只是使用** Distinct **来从CTE中获取数据。 –

+0

是的,我了解有关截然不同的东西,让我确切地期望重现。但我对CTE感到困惑。我是初学者到SQL服务器。我没有从CTE获取相同的特征。 –

0

请改变你的WHERE条件如下:

select 
bve.EmployerContribution, 
bhsac.CatchUpValue as CatchUpValue , 
bcl.Tier, 
bcl.planYear, 
bhsac.Ischecked, 
isnull(bhsac.Value,0) as EmployeeContribute, 
Id=(convert(varchar, bcl.Id) + '$' + convert(varchar, isnull(bhsac.Id, 0))) , 
bhsac.Value , 
bhsac.HSALmitId 
from  
dbo.benContributionStructure bcs 
inner join dbo.benVariableElection bve on bcs.PlanInfoId = bve.PlanInfoId 
inner join dbo.benBenefitContributionLimit bcl on bcs.SavingCategory =  bcl.CategoryID 
left outer join dbo.benBenefitHSACoverage bhsac on bcs.PlanInfoId = bhsac.planInfoId 
     and bcl.Id=bhsac.HSALmitId --and [email protected] 

left outer join dbo.benEmployeeContribution bec on bhsac.UserID = bec.UserId and bhsac.BenefitClassId = bec.BenefitClassId -- and bec.EnrollmentType !='Closed' 
left outer join benOpenEnrollment oems on oems.ID = bec.OpenEnrollmentId 
where  
bcs.PlanInfoId=38044 and bcl.Ischecked=1 
         and bcl.Tier !='CatchUp'  
    and bcl.CompanyId=1629 
    and bhsac.UserID='d76c5000-69e0-461e-92e1-3cfe7590d098' 
    and bhsac.PlanInfoId=38044 
    and oems.EndDt > GETDATE()