Sql server 获取同一表中两列的计数时,未显示第二列的正确计数

Sql server 获取同一表中两列的计数时,未显示第二列的正确计数,sql-server,Sql Server,我的存储过程如下所示: 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 ,

我的存储过程如下所示:

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
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' 
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'
我这样传递参数: @Ecode='E003'@startdate='2013-09-03',@enddate='2013-09-03'

我是这样输出的:但是cntDelEcode出错了。我没有正确地计算DelEcode的数量,所以我必须在存储过程中更改什么

用于检查CntEcode i worte查询的计数,如下所示:

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
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' 
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'
。现在我得到了27行。所以我知道我的cntEcode计数是正确的。 要检查CntDelEcode的计数,请执行如下查询:

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
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' 
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行..我的存储过程哪里出错了?请帮助我找出

在您得到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 
     ...

在我的存储过程中,我想得到CntDelEcode的计数是35。因此,当我在测试是否给定了不同的日期时,如何重新写入IR,,没有得到正确的答案:我给出了如下查询:从事务中选择计数*,其中dtime>=+@date1+和dtime