0

我的存储过程是这样的:

ALTER procedure [dbo].[Driverperformance] 
 @Ecode nvarchar(50),  
 @startdate datetime,
 @enddate datetime  as
begin  
    SELECT e.Ecode,CAST(q.dtime AS DATE) as Date , 
           e.Ename, 
           count(q.Ecode) CntEcode ,
           count(q.DelEcode) CntDelEcode
    FROM EmployeeMaster_tbl e 
     inner JOIN Transaction_tbl q  
      ON e.Ecode = q.Ecode 
    where q.Ecode=@Ecode
     and dtime between '' + @startdate +'' and ''+@enddate+'' 
    group by e.Ecode, e.Ename, CAST(q.dtime AS date) 
    ORDER BY CAST(q.dtime AS date)--e.Ecode DESC
end

我像这样传递了我的参数: @Ecode = 'E003' @startdate = '2013-09-03', @enddate = '2013-09-03'

在此处输入图像描述

我是这样出来的:但是cntDelEcode出错了。(我没有正确计算DelEcode)所以我必须在存储过程中进行更改)

为了检查 CntEcode 的计数,我写了这样的查询:

select * from Transaction_tbl where dtime >='2013-09-03 00:00:00.000' and dtime <='2013-09-03 23:59:59.000' and Ecode='E003' 

. 现在我得到了 27 行。所以我知道我的 cntEcode 计数是正确的。为了检查 CntDelEcode 的计数,我写了这样的查询:

select * from Transaction_tbl where dtime >='2013-09-03 00:00:00.000' and dtime <='2013-09-03 23:59:59.000' and DelEcode='E003'

现在我得到了 35 行..但是在执行我的存储过程时,我只得到了 23 行,而不是 35 行..我的存储过程哪里出错了?请帮我找出答案

4

1 回答 1

0

在获得 35 行的查询中,您正在计算其中的行DelECode='E0003'

这是与上述查询不同的查询。难怪结果不同。

您可以将查询更改为

SELECT e.Ecode,CAST(q.dtime AS DATE) as Date , 
       e.Ename, 
       count(q.Ecode) CntEcode ,
       (select count(*) from Transaction_tbl 
               where q.Ecode=@Ecode
               and dtime between @startdate and @enddate) CntDelEcode
FROM EmployeeMaster_tbl e 
     ...
于 2013-09-04T09:48:01.030 回答