nested select statement help
I am having trouble displaying the number of opencases that are found in my stored procedure, I am not sure if that select statement is placed properly.
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
C.CaseNumber
, O.OfficeName
, CT.Description AS CaseType,
C.DateOpened AS DateOpened,
CR.Description AS Court
FROM
(
SELECT C.CaseId, O.OfficeId FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
WHERE OD.OfficeId = O.OfficeId
AND
( CD.DateOpened BETWEEN @BeginDate AND @EndDate
OR
CSC.DateReopened BETWEEN @BeginDate AND @EndDate
)
)AS OpenCases
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
)
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
OpenCases should occur before the Join
statements as OpenCases is a data Column
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
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
FROM
(
SELECT C.CaseId, O.OfficeId,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
WHERE OD.OfficeId = O.OfficeId
AND
( CD.DateOpened BETWEEN @BeginDate AND @EndDate
OR
CSC.DateReopened BETWEEN @BeginDate AND @EndDate
)
)AS OpenCases
FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
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
)
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
精彩评论