在我的存储过程中,我正在检查日期范围内的打开案例,以及仅基于开始日期和开始日期之前的打开案例。我尝试将所有选择添加到一个存储过程中,但是当我执行代码时 - 对于开始日期的打开案例,返回的所有结果都为null。这是我的完整存储过程和select语句AS OpenBeginCases是我遇到麻烦的地方。
SELECT
C.CaseNumber,
O.OfficeName,
CT.Description AS CaseType,
DATEADD(dd, 0, DATEDIFF(dd, 0, C.DateOpened)) AS DateOpened,
CR.Description AS Court,
CaseOfficeAppointment.OpenCases,
CaseOfficeAppointment.CloseCases,
CaseOfficeAppointment.OpenBeginCases
FROM
(
SELECT C.CaseId, O.OfficeId, CRT.CourtId,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
--INNER JOIN Court CR ON CD.CourtId = CR.CourtId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
--WHERE CR.CourtId = CRT.CourtId
WHERE OD.OfficeId = O.OfficeId
AND
( CD.DateOpened BETWEEN @BeginDate AND @EndDate
OR
CSC.DateReopened BETWEEN @BeginDate AND @EndDate
)
)AS OpenCases,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
--INNER JOIN Court CR ON CD.CourtId = CR.CourtId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
--WHERE CR.CourtId = CRT.CourtId
WHERE OD.OfficeId = O.OfficeId
AND
( CSC.DateClosed BETWEEN @BeginDate AND @EndDate
)
)AS CloseCases,
(
SELECT SUM(CaseCount)AS Counts
FROM (
SELECT COUNT(C.CaseId) AS CaseCount,O.OfficeId
FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
WHERE C.DateCreated <= @BeginDate
AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < @BeginDate)
GROUP BY O.OfficeId
UNION
-- Also need the cases that reopened and are currently open
SELECT COUNT(ReOpened.CaseId) As CaseCount, ReOpened.OfficeID
FROM (
SELECT C.CaseId, MAX(CSC.DateReopened) AS DateReOpened, O.OfficeId
FROM [Case] C
INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
WHERE CSC.DateReopened <= @BeginDate
GROUP BY C.CaseId, O.OfficeID
) AS ReOpened
WHERE ReOpened.CaseId NOT IN
(
SELECT CaseId FROM CaseStatusChange
WHERE CaseId = ReOpened.CaseId AND
CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND @BeginDate
)
GROUP BY ReOpened.OfficeId
) AS OpenCasesCount
GROUP BY OfficeId
)AS OpenBeginCases
FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
INNER JOIN [Court] CRT ON C.CourtId = CRT.CourtId
WHERE
-- Case was open (or reopened) during the date range
C.DateOpened BETWEEN @beginDate AND @endDate
OR
C.CaseId IN (SELECT CaseId FROM CaseStatusChange WHERE DateReopened BETWEEN @beginDate AND @endDate)
AND
-- Office had an appointment sometime during the date range
A.DateOn < @endDate AND (A.DateOff IS NULL OR A.DateOff BETWEEN @beginDate AND @endDate)
GROUP BY C.CaseId, O.OfficeId, CRT.CourtId
)
CaseOfficeAppointment
INNER JOIN [Case] C ON CaseOfficeAppointment.CaseId = C.CaseId
INNER JOIN [Office] O ON CaseOfficeAppointment.OfficeId = O.OfficeId
INNER JOIN [CaseType] CT ON C.CaseTypeId = CT.CaseTypeId
INNER JOIN [Court] CR ON C.CourtId = CR.CourtId
答案 0 :(得分:0)
UNION
换成UNION ALL
?
不要没关系,这无关紧要。
答案 1 :(得分:0)
为了清晰起见,尝试将您的脚本清理成CTE格式,或许您可以在此之后获得更好的洞察力。