将多个结果连接到sql server中的一个结果

时间:2013-09-05 09:04:19

标签: sql-server

我有这样的存储过程:

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

我怎样才能做到这一点?如果有人知道请帮我查一查。

2 个答案:

答案 0 :(得分:1)

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

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表中的值也可以为null:

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

答案 1 :(得分:0)

看起来这两个表中都缺少日期,所以FULL 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