0

我有一张这样的桌子:

Transactid   dtime       Ecode    DelEcode
1           2013-08-01   E003      null
2           2013-08-01   E003      E003
3           2013-08-02   E001
4           2013-08-02   E001

我想计算特定日期和Ecode名称。现在我得到答案,如果特定的两者都和 我的存储过程是这样的:DelEcodeEcodeEcodeEcodeDelEcode

ALTER PROCEDURE [dbo].[Driverperformanceforalldriver] @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) ;

 ;

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 in
       (SELECT e.Ecode
        FROM dbo.EmployeeMaster_tbl e
        WHERE e.gid NOT IN
            (SELECT did
             FROM dbo.Designation_tbl d
             WHERE d.Dname ='Driver'))
     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) CntDelEcode
   FROM employeemaster_tbl e
   JOIN transaction_tbl q ON e.ecode = q.delecode
   WHERE q.delecode IN
       (SELECT e.Ecode
        FROM dbo.EmployeeMaster_tbl e
        WHERE e.gid NOT IN
            (SELECT did
             FROM dbo.Designation_tbl d
             WHERE d.Dname ='Driver'))
     AND dtime >= '' + @date1 + ''
     AND dtime <= '' + @date2 + ''
   GROUP BY e.ecode,
            e.ename,
            Cast(q.dtime AS DATE))
SELECT del.ecode,
       del.[Date],
       del.ename,
       ISNULL(cnt.cntEcode, 0) CntEcode ,
       ISNULL(del.CntDelEcode, 0) AS CntDelEcode
FROM cnt FULL
JOIN del ON del.ecode = cnt.ecode
AND del.[Date] = cnt.[Date]
AND del.Ename = cnt.Ename
ORDER BY del.date END

执行后,我得到这个输出:

Ecode    dtime       Ename     cntEcode  CntDelCode
E003     2103-08-01  raheem     2        1
null     null        null       2        0

如果有的话,DelEcode那么只有它显示其他细节(我的意思是如果有的E001DelEcode工作正常。我的存储过程有什么问题?

Ecode    dtime      Ename      cntEcode CntDelCode
E003     2103-08-01  raheem     2        1
E001     2103-08-02  jaseem     2        0
4

1 回答 1

0

这是select程序的一部分:

SELECT del.ecode,
       del.[Date],
       del.ename,
       ISNULL(cnt.cntEcode, 0) CntEcode ,
       ISNULL(del.CntDelEcode, 0) AS CntDelEcode
FROM cnt FULL JOIN
     del
     ON del.ecode = cnt.ecode AND del.[Date] = cnt.[Date] AND del.Ename = cnt.Ename;

你正在做一个full join然后从一个表中提取数据。该表中可能没有数据。所以,使用coalesce()

SELECT coalesce(del.ecode, cnt.ecode) as ecode,
       coalesce(del.[Date], cnt.[Date]) as [Date],
       coalesce(del.ename, cnt.Ename) as Ename,
       coalesce(cnt.cntEcode, 0) as CntEcode,
       coalesce(del.CntDelEcode, 0) as CntDelEcode
FROM cnt FULL JOIN
     del
     ON del.ecode = cnt.ecode AND del.[Date] = cnt.[Date] AND del.Ename = cnt.Ename;
于 2013-09-16T11:16:14.377 回答