具有多个连接的重复记录

时间:2017-01-03 05:01:06

标签: sql-server

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

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 

为此,我将结果作为副本获得第二行:

observe the result

2 个答案:

答案 0 :(得分: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 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

答案 1 :(得分:0)

请更改以下条件:

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {
        if (requestCode==this.requestCode_permission_CALL_PHONE)
        {
            if (permissions[0].equals(Manifest.permission.CALL_PHONE)) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED)
                    {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + this.getString(R.string.phone_number)));
                        this.startActivity(intent);
                    }
                } else {
                    Log.d("permission","permission grant ignored by user");
                    //requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSIONS_CODE);
                }
            }
        }
    }
    public int requestCode_permission_CALL_PHONE=1001;

    public void doCall(View v) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CALL_PHONE} , this.requestCode_permission_CALL_PHONE);
            return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + this.getString(R.string.phone_number)));
        this.startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}