如果我有每位员工的“总分配假”列,如何计算请假余额?

时间:2019-10-09 05:29:31

标签: c# asp.net

我有empdetails表,其中列为分配的叶子。我还有一个用于LeavesTaken的专栏。现在,我要计算每个员工的请假余额并相应地进行更新。

        {
            command.Connection = _connection;
            SqlDataReader reader = command.ExecuteReader();
            Employee employee = new Employee();
            List<Users.LeaveRequest> leaves = new List<Users.LeaveRequest>();
            while (reader.Read())
            {
                leaves.Add(new Users.LeaveRequest() { DayPart = reader.GetInt32(3), fromdate = reader["fromdate"].ToString(), todate = reader["todate"].ToString() });
            }
            reader.Close();
            if (leaves.Count>0)
            {
                leaveRequests = leaves;
            }
            else
            {
                leaveRequests = null;
            }

            command.CommandText = "SELECT TotalLeaves,LeavesTaken FROM empDetails WHERE empID = @empid";
            //command.Parameters.AddWithValue("@empid", CurrentUser.Employee.EmpID);
            reader = command.ExecuteReader();
            if (reader.Read())
            {
                employee.TotalLeaves = reader.GetInt32(0);
                employee.LeavesTaken = reader.GetDouble(1);
            }
            reader.Close();
            return employee;
        }````

2 个答案:

答案 0 :(得分:0)

我可以想到三种解决方案,因为您没有精确指定所需的内容,也没有示例数据:

  1. 您想要总计员工的价值:

    SELECT SUM(TotalLeaves), SUM(LeavesTaken)
    FROM empDetails 
    WHERE empID = @empid
    
  2. 您要在两个值之间取得平衡

    SELECT SUM(TotalLeaves) - SUM(LeavesTaken)
    FROM empDetails 
    WHERE empID = @empid
    
  3. 如果每个empID有一条记录,您还可以尝试:

    SELECT TotalLeaves - LeavesTaken
    FROM empDetails 
    WHERE empID = @empid
    

答案 1 :(得分:0)

我很好奇为什么总叶子有整数,但是无论如何,您都可以在CommandText

中进行计算

假设这三列AllottedLeaves, TotalLeaves, LeavesTaken的数据类型为DECIMAL(4,2)

  

现在,我想计算每位员工的请假余额并相应地进行更新。

  1. 更新TABLE empDetails

    中的所有员工
    {
        command.CommandText = "UPDATE empDetails " + 
                              "SET TotalLeaves = TotalLeaves - llottedLeaves, " +
                              "LeavesTaken = LeavesTaken + AllottedLeaves, " +
                              "AllottedLeaves = 0" +
                              "WHERE AllottedLeaves <> 0 AND AllottedLeaves <= TotalLeaves";
        command.Connection = _connection;
        command.ExecuteNonQuery();
    }
    
  2. 更新TABLE empDetails中的一名员工

    {
        command.CommandText = "UPDATE empDetails " + 
                              "SET TotalLeaves = TotalLeaves - llottedLeaves, " +
                              "LeavesTaken = LeavesTaken + AllottedLeaves, " +
                              "AllottedLeaves = 0" +
                              "WHERE empID = @empid";
        command.Parameters.AddWithValue("@empid", CurrentUser.Employee.EmpID);
        command.Connection = _connection;
        command.ExecuteNonQuery();
    }