1

我有一个这样的存储过程:

ALTER PROCEDURE [dbo].[Driverperformance] 
   @Ecode NVARCHAR(50),
   @startdate DATE,
   @enddate DATE 
AS BEGIN
   SELECT 
      e.Ecode, q.dtime, e.Ename, 
      count(q.Ecode) CntEcode, 
      count(DelEcode) CntDelEcode, 
      CAST((count(q.Ecode) + count(DelEcode)) as float)/2 as Avrgpoint
   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, q.dtime 
   ORDER BY 
      e.Ecode DESC
END

执行此操作时:输出是这样的:

Ecode   dtime                     Ename              CntEcode  CntDelEcode   Avrgpoint
E001    2013-05-25 12:55:23.000   Deepu M. Thomas       1           0           0.5
E001    2013-05-25 13:53:16.000   Deepu M. Thomas       1           0           0.5
E001    2013-05-27 03:24:59.000   Deepu M. Thomas       1           1           0.5
E001    2013-05-27 03:24:59.000   Deepu M. Thomas       1           0           0.5
E001    2013-05-28 03:24:59.000   Deepu M. Thomas       1           0           0.5

我想为每个日期获取(CntEcode,CntDelEcode)的输出总数。预期输出如下:

Ecode   dtime         Ename               CntEcode    CntDelEcode 
E001    2013-05-25    Deepu M. Thomas        2            0            
E001    2013-05-27    Deepu M. Thomas        2            1          
E001    2013-05-28    Deepu M. Thomas        1            0           
4

1 回答 1

0

如果您只想查看 的日期部分dtime,请使用CAST(dtime AS DATE)仅获取日期:

ALTER PROCEDURE [dbo].[Driverperformance] 
   @Ecode NVARCHAR(50),
   @startdate DATE,
   @enddate DATE 
AS BEGIN
   SELECT 
      e.Ecode, 
      CAST(q.dtime AS DATE),      -- <== CAST to DATE
      e.Ename, 
      count(q.Ecode) CntEcode, 
      count(DelEcode) CntDelEcode, 
      CAST((count(q.Ecode) + count(DelEcode)) as float)/2 as Avrgpoint
   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)    -- <== CAST to DATE here, too!
   ORDER BY 
      e.Ecode DESC
END
于 2013-07-28T08:46:52.873 回答