1

我的存储过程是这样的:

ALTER PROCEDURE [dbo].[Driverperformance] @Ecode     NVARCHAR(50), 
                                          @startdate DATETIME, 
                                          @enddate   DATETIME 
AS 
  BEGIN 
      DECLARE @date1 DATETIME = CONVERT(DATETIME, @startdate + ' 00:01:00.000', 
                                120); 
      DECLARE @date2 DATETIME = CONVERT(DATETIME, @enddate + ' 23:23:59.000', 
                                120) 
      ; 

      SELECT e.ecode, 
             Cast(q.dtime AS DATE) AS Date, 
             e.ename, 
             Count(q.ecode)        CntEcode 
      FROM   employeemaster_tbl e 
             JOIN transaction_tbl q 
               ON e.ecode = q.ecode 
      WHERE  q.ecode = @Ecode 
             AND dtime >= '' + @date1 + '' 
             AND dtime <= '' + @date2 + '' 
      UNION 
      SELECT e.ecode, 
             Cast(q.dtime AS DATE) AS Date, 
             e.ename, 
             Count(q.delecode)     DelEcode 
      FROM   employeemaster_tbl e 
             JOIN transaction_tbl q 
               ON e.ecode = q.delecode 
      WHERE  q.delecode = @Ecode 
             AND dtime >= '' + @date1 + '' 
             AND dtime <= '' + @date2 + '' 
      GROUP  BY e.ecode, 
                e.ename, 
                Cast(q.dtime AS DATE) 
      ORDER  BY Cast(q.dtime AS DATE) 
  END 

执行此操作时出现错误:

列 'EmployeeMaster_tbl.Ecode' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

4

1 回答 1

2

GROUP BY您在之前的代码段中错过了UNION

ALTER PROCEDURE [dbo].[Driverperformance] @Ecode     NVARCHAR(50), 
                                          @startdate DATETIME, 
                                          @enddate   DATETIME 
AS 
  BEGIN 
      DECLARE @date1 DATETIME = CONVERT(DATETIME, @startdate + ' 00:01:00.000', 
                                120); 
      DECLARE @date2 DATETIME = CONVERT(DATETIME, @enddate + ' 23:23:59.000', 
                                120) 
      ; 

      SELECT e.ecode, 
             Cast(q.dtime AS DATE) AS Date, 
             e.ename, 
             Count(q.ecode)        CntEcode 
      FROM   employeemaster_tbl e 
             JOIN transaction_tbl q 
               ON e.ecode = q.ecode 
      WHERE  q.ecode = @Ecode 
             AND dtime >= '' + @date1 + '' 
             AND dtime <= '' + @date2 + ''
      GROUP  BY e.ecode, 
                e.ename, 
                Cast(q.dtime AS DATE)              
      UNION 
      SELECT e.ecode, 
             Cast(q.dtime AS DATE) AS Date, 
             e.ename, 
             Count(q.delecode)     DelEcode 
      FROM   employeemaster_tbl e 
             JOIN transaction_tbl q 
               ON e.ecode = q.delecode 
      WHERE  q.delecode = @Ecode 
             AND dtime >= '' + @date1 + '' 
             AND dtime <= '' + @date2 + '' 
      GROUP  BY e.ecode, 
                e.ename, 
                Cast(q.dtime AS DATE) 
      ORDER  BY Cast(q.dtime AS DATE) 
  END 

拉吉

于 2013-09-05T07:45:21.930 回答