简体   繁体   中英

Stored procedure wrong output

I have this stored procedure that returns data on a selected week, as in the Company , Name , Expected Work , and Work done between the 19/10/2015 and the 25/10/2015 (per example).

I have just recently added the Expected Work line and for some odd reason the output differs from one week to another, when the value should be the same.

Company1 - Christopher - 35 - 35 | On one week can give the following on another :

Company1 - Christopher - 350 - 35

I have just realized the value isn't correct when there is a value to Work done , if there is no work recorded the Expected Work has the right value.

Here is the procedure :

ALTER procedure [dbo].[spGetWeeklyActivityByEmployee]
      @startDate date
    , @endDate date
as
    set datefirst 1 -- Monday

    select 
        Company.Name as [Company]
        , Employee.FirstName + ' ' + Employee.LastName as [Name]        
        , sum(UserActivity.Cost) as [Recorder Time]
        , sum(Employee.ExpectedTime) as [Expected Time] // I have added this line, not sure if it's correct
    from 
        dbo.Employee
    inner join 
        dbo.Company on Company.CompanyId = Employee.CompanyId
    left join 
        dbo.UserActivity on UserActivity.Login = Employee.Login
                         and UserActivity.CalendarDate >= @startDate
                         and UserActivity.CalendarDate <= @endDate
    where 
        (Employee.EntranceDate is null
         or YEAR(Employee.EntranceDate) < YEAR(@startDate)
         or (YEAR(Employee.EntranceDate) = YEAR(@startDate) 
         and DATEPART(WEEK, Employee.EntranceDate) <= DATEPART(WEEK, @startDate)))
        and (Employee.ExitDate is null
             or YEAR(Employee.ExitDate) > YEAR(@endDate)
             or (YEAR(Employee.ExitDate) = YEAR(@endDate) 
             and DATEPART(WEEK, Employee.ExitDate) >= DATEPART(WEEK, @endDate)))
    group by 
        Company.Name, Employee.FirstName + ' ' + Employee.LastName

    return 0

Am I missing something? Is the way I retrieve Expected Time wrong?

EDIT :

Here is the part in the code where I save the information in an array :

// create and open a connection object
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();

// 1.  create a command object identifying
//     the stored procedure
SqlCommand cmd = new SqlCommand("spGetWeeklyActivityByEmployee", conn);

// 2. set the command object so it knows
//    to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which
//    will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@startDate", wk1));
cmd.Parameters.Add(new SqlParameter("@endDate", wk2));

// execute the command
rdr = cmd.ExecuteReader();         

string[] tab_company = new string[31]; // Don't mind the sizes
string[] tab_name = new string[31];
string[] tab_expectedtime = new string[31];
string[] tab_rectime = new string[31];

int counter;

counter = 0;

while (rdr.Read())
{
    tab_company[counter] = rdr["Company"].ToString();
    tab_name[counter] = rdr["Name"].ToString();
    tab_expectedtime[counter] = rdr["Expected Time"].ToString();
    tab_rectime[counter] = rdr["Recorder Time"].ToString();

    counter++;               
}

Perhaps the change in value comes from here?

只需从Employee.ExpectedTime删除SUM()并将其添加到您的GROUP BY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM