嵌套的select语句帮助

时间:2011-06-07 17:12:10

标签: stored-procedures

我无法显示在我的存储过程中找到的opencase数量,我不确定是否正确放置了select语句。

    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

1 个答案:

答案 0 :(得分:1)

OpenCases应该出现在Join语句之前,因为OpenCases是一个数据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