如何获取sql中每个日期的特定值的总计数

时间:2013-07-28 08:09:36

标签: sql-server

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

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

我想获得每个日期的输出totalcount(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           

1 个答案:

答案 0 :(得分: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