我有这样的商店程序:
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)
ORDER BY 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) --e.Ecode DESC
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
我怎么能做到这一点?如果有人知道请帮助我找出答案。
答案 0 :(得分:1)
您可以使用UNION或UNION ALL运算符来加入相同的结果集。
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+''
UNION
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)
答案 1 :(得分:1)
尝试这样的事情,只需确保修改为top select语句以选择所需的数据
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 T1.*, T2.*
FROM
(
SELECT e.Ecode as ECode,
CAST(q.dtime AS DATE) as [Date],
e.Ename as 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)
) T1
FULL JOIN
(
SELECT e.Ecode as ECode,
CAST(q.dtime AS DATE) as [Date],
e.Ename as 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)
) T2
ON T1.ECode = T2.ECode
ORDER BY T1.Date
END