次のようなテーブルがあります。
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
特定の日付、および の名前を取得したいEcode
。Ecode
特定の両方が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