Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:06:15+00:00 2026-05-23T05:06:15+00:00

Within my stored procedure i am checking for open cases within a date range

  • 0

Within my stored procedure i am checking for open cases within a date range and also for open cases based only on the begin date and before the begin date. I tried to add all the selects into one stored procedure but when i execute the code – for the open cases on begin date all the results returned are null. Here is my full stored procedure and the select statement AS OpenBeginCases is where i am running into trouble.

 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:06:16+00:00Added an answer on May 23, 2026 at 5:06 am

    Try to clean up your script into CTE format for clarity, perhaps you’ll get a better insight after that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you're creating a temporary table within a stored procedure and want to add
Within a stored procedure, another stored procedure is being called within a cursor. For
I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND
I am attempting to execute a Stored Procedure within another stored procedure. The catch
I have a result set in MS-SQL within a stored procedure, and lets say
When making an HttpWebRequest within a CLR stored procedure (as per the code below),
I need to access the result of a stored procedure within a select statement,
I have a block of code that is repeated within a DB2 stored procedure.
My stored procedure is called as below from an SQL instegartion package within SQL
I have a stored procedure that I want to call from within another, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.