我在下面的存储过程中链接了以下表格。
A
------
ACCOUNT_DAY
MARK_TIME
MARKTYPE
PLAC_ID
B
------
EMPLOYNO
RES_ID
C
------
PLAC_ID
RES_ID
D
------
CAUSE_ID
PLAC_ID
E
------
CAUSE_ID
CAUSE
存储过程代码:
ALTER PROCEDURE [dbo].[startstoptime]
@employno varchar(5),
@startdate datetime,
@enddate datetime,
@enddate2 datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET @enddate = DATEADD (d, 1, CAST(@startdate AS datetime));
SET @enddate2 = DATEADD (d, 7, CAST(@startdate AS datetime));
-- Insert statements for procedure here
SELECT DISTINCT B.EMPLOYNO, A.ACCOUNT_DAY,
(SELECT DISTINCT A.MARK_TIME
FROM SYSADM.MARK AS A INNER JOIN
SYSADM.PLACING AS C ON A.PLAC_ID = C.PLAC_ID INNER JOIN
SYSADM.STAFF_RESOURCE AS B ON B.RES_ID = C.RES_ID INNER JOIN
SYSADM.ABSENCE AS D INNER JOIN
SYSADM.ABSENCE_CAUSE AS E ON D.CAUSE_ID = E.CAUSE_ID ON C.PLAC_ID = D.PLAC_ID AND B.RES_ID = D.RES_ID
WHERE (A.MARKTYPE = '1') AND (B.EMPLOYNO = @employno) AND (A.ACCOUNT_DAY >= CONVERT(DATETIME, @startdate, 102)) AND
(A.ACCOUNT_DAY < CONVERT(DATETIME, @endDate, 102)) AND (E.EXP_DESC IS NULL)) AS start,
(SELECT DISTINCT A.MARK_TIME
FROM SYSADM.MARK AS A INNER JOIN
SYSADM.PLACING AS C ON A.PLAC_ID = C.PLAC_ID INNER JOIN
SYSADM.STAFF_RESOURCE AS B ON B.RES_ID = C.RES_ID INNER JOIN
SYSADM.ABSENCE AS D INNER JOIN
SYSADM.ABSENCE_CAUSE AS E ON D.CAUSE_ID = E.CAUSE_ID ON C.PLAC_ID = D.PLAC_ID AND B.RES_ID = D.RES_ID
WHERE (A.MARKTYPE = '2') AND (B.EMPLOYNO = @employno) AND (A.ACCOUNT_DAY >= CONVERT(DATETIME, @startdate, 102)) AND
(A.ACCOUNT_DAY < CONVERT(DATETIME, @endDate, 102)) AND (E.EXP_DESC IS NULL)) AS stopp
FROM SYSADM.MARK AS A INNER JOIN
SYSADM.PLACING AS C ON A.PLAC_ID = C.PLAC_ID INNER JOIN
SYSADM.STAFF_RESOURCE AS B ON B.RES_ID = C.RES_ID INNER JOIN
SYSADM.ABSENCE AS D INNER JOIN
SYSADM.ABSENCE_CAUSE AS E ON D.CAUSE_ID = E.CAUSE_ID ON C.PLAC_ID = D.PLAC_ID AND B.RES_ID = D.RES_ID
WHERE (E.EXP_DESC IS NULL)
GROUP BY B.EMPLOYNO, A.ACCOUNT_DAY, A.MARK_TIME, E.EXP_DESC, A.MARKTYPE
HAVING (B.EMPLOYNO = @employno) AND (A.ACCOUNT_DAY >= CONVERT(DATETIME, @startdate, 102)) AND (A.ACCOUNT_DAY < CONVERT(DATETIME,
@endDate2, 102))
END
结果如下:
EMPLOYNO | ACCOUNT_DAY | start | stop
------------+---------------------------+---------------------------+---------------------------
03064 | 2015-02-05 00:00:00.000 | 2015-02-05 05:21:00.000 | 2015-02-05 14:15:00.000
03064 | 2015-02-09 00:00:00.000 | 2015-02-05 05:21:00.000 | 2015-02-05 14:15:00.000
03064 | 2015-02-11 00:00:00.000 | 2015-02-05 05:21:00.000 | 2015-02-05 14:15:00.000
启动和停止给出了错误的答案,因为我不知道如何在子查询中获取ACCOUNT_DAY而不是开始和停止日期,而不会得到多行的结果。