从员工自我加入中删除重复项

时间:2015-08-14 16:53:09

标签: tsql self-join

我有一张员工表,所有员工都在这里。我需要用相应的主管提取一部分员工。该表看起来类似于:

Emp_id | F_name | L_name | Superv_id  | Superv_flg
---------------------------------------------------
123      john      doe      456          N
456      jane      doe      278          Y
234      Jack      smith    268          N

到目前为止,我的查询看起来像这样:

with cte as 
(
    select f_name + ' ' l_name as supervisor, superv_id, emp_id
        from [dbo].[SAP_worker_all]
        where supvr_flag = 'Y'
    )
    SELECT distinct w.[first_name]
           ,w.[last_name]
           ,cte.supervisor
FROM [dbo].[SAP_worker_all] w
join  cte
        on w.[superv_id] = cte.[superv_id];

我收到重复的值,并且返回的主管不是正确的值。我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果empID是唯一的,则不应该有重复项

 namespace Inventory
{
    public partial class Results : System.Web.UI.Page
    {
        //SqlConnection conn = new SqlConnection("Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=ActioNet1234");
        private String ConnString = ConfigurationManager.ConnectionStrings["ActioNetITInventoryConnectionString"].ConnectionString;


        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                //BindData();
                //BindData2();
                //BindData3();



            }//end if


        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            BindData();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();

        }//end page load

        protected void BindData()
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT * FROM [Inventory]";
                    cmd.Connection = sqlCon;
                    sqlCon.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);




                    GridView1.DataSourceID = null;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    sqlCon.Close();

                }//end using
            }//end using

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var serial = GridView1.DataKeys[e.RowIndex].Value;
            GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
            //getting row field details

            TextBox Assigned = row.FindControl("txtAssigned") as TextBox;
            TextBox Location = row.FindControl("txtLocation") as TextBox;
            TextBox Notes = row.FindControl("txtNotes") as TextBox;

            using (SqlConnection sqlCon = new SqlConnection(ConnString))
            {
                string sql = "UPDATE Inventory SET Assigned=@Assigned, " + "Location=@Location, Notes=@Notes" + " WHERE Serial = @Serial";

                using (SqlCommand cmd = new SqlCommand(sql, sqlCon))
                {

                    cmd.Parameters.AddWithValue("@Assigned", Assigned.Text.Trim());
                    cmd.Parameters.AddWithValue("@Location", Location.Text.Trim());
                    cmd.Parameters.AddWithValue("@Notes", Notes.Text);
                    cmd.Parameters.AddWithValue("@Serial", serial);

                    sqlCon.Open();
                    cmd.ExecuteNonQuery();
                    sqlCon.Close();



                }//end using
            }//end using


            status.Visible = true;
            status.Text = "" + Assigned.Text + "has been added successfully!";
            status.ForeColor = System.Drawing.Color.Green;


            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            GridView1.EditIndex = -1;
            BindData();
        }