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の数とDelEcode特定の日付、および の名前を取得したいEcodeEcode特定の両方がEcodeあり、次のDelEcode ような My ストアド プロシージャがある場合、今、私は答えを得ています。

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のみ、他の詳細が表示されます(ある場合は正常に動作することを意味しE001ましDelEcodeた。ストアドプロシージャの何が問題になっていますか?

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してから、1 つのテーブルのみからデータを引き出しています。そのテーブルにはデータがない可能性があります。したがって、次を使用します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 に答える