0

我有这样的存储过程:

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)  

     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

我得到第一个结果:

Ecode   Date         Ename    CntEcode  
E003    2013-05-05   Raheem    4                    
E003    2013-05-28   Raheem    1 

第二个结果:

Ecode   Date         Ename    CntEcode  
E003    2013-05-05   Raheem    3         
E003    2013-05-27   Raheem    7           
E003    2013-05-28   Raheem    1

但我想得到这样的结果:

Ecode   Date         Ename    CntEcode   DelEcode
E003    2013-05-05   Raheem    4            3
E003    2013-05-27   Raheem    0            7
E003    2013-05-28   Raheem    1            1

我怎样才能做到这一点?如果有人知道,请帮助我找出答案。

4

2 回答 2

1

我会使用 TI 在第一部分中发布的内容:

WITH firstResult AS
(
SELECT 
      e.ecode,   
      Cast(q.dtime AS DATE) AS DateAdded,   
      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)  
), 
secondResult AS
(
 SELECT 
      e.ecode, 
      Cast(q.dtime AS DATE) AS DateAdded, 
      e.ename, 
      Count(q.delecode) as 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) 
)

但是对于 Ecode、DateAdded 和 EName 也使用 ISNULL,因为我们有一个完全连接,并且 secondResult 表中的值也可以为空:

select ISNULL(firstResult.Ecode,secondResult.Ecode) as Ecode,ISNULL(firstResult.DateAdded,secondResult.DateAdded) as DateAdded,  
    ISNULL(firstResult.Ename,secondResult.Ename) as Ename, ISNULL(firstResult.EntEcode,0) as Ecode, ISNULL(secondResult.DelEcode,0) as DelEcode
    from
    firstResult full outer join secondResult on firstResult.Ecode = secondResult.Ecode and firstResult.DateAdded = secondResult.DateAdded
    order by DateAdded
于 2013-09-05T09:37:08.700 回答
0

看起来任何一个表中都可能缺少日期,因此 aFULL JOIN似乎更合适并用于ISNULL获取0当天的计数为空的时间

尝试:

; WITH cnt AS
(
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)  
), 
del AS
(
 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) 
)

SELECT ISNULL(del.ecode, ctn.ecode) ecode
       , ISNULL(del.[Date], cnt.[Date]) [Date]
       , ISNULL(del.ename, cnt.ename) ename
       , ISNULL(cnt.cntEcode, 0) CntEcode
       , ISNULL(del.cntEcode, 0) AS DelEcode
FROM cnt
FULL JOIN del ON del.ecode = cnt.ecode
             AND del.[Date] = cnt.[Date]
             AND del.Ename = cnt.Ename
于 2013-09-05T09:26:33.823 回答