6

Duplicate records with multiple joins

 3 years ago
source link: https://www.codesd.com/item/duplicate-records-with-multiple-joins.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Duplicate records with multiple joins

advertisements

I had written a join query statement. that statement returns me multiple(duplicate) row again even though I'm having only single record on that.

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 bhsac.BenefitClassId=@BenefitClass
        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

For that I'm getting the result as second row as duplicate :

observe the result


Try this once it may help you

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 bhsac.BenefitClassId=@BenefitClass
        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

Tags sql-server

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK