Jump to content
  • 0

Gap in Dates - Or Next Date Due


MacCaspio

Question

Hello.  I have a scheduling application that ensures a task is manually added to the schedule every day.  We operate the manual scheduling process every few days.  I need to figure out a way to calculate the next opening in the calendar.  It needs to return the first "gap" it finds as it counts from today forward....not the date that is the furthest out.  

Example:  If we have a task scheduled for each of the next 3 days, I need to find the gap that exists starting on day 4 and return that date.  This will allow the scheduler to know how far out the schedule has been created. 

ChatGPT says I should run the following SQL queries in a calculated field, but I don't believe Caspio will allow these subqueries.  Does anyone have any ideas how to accomplish finding the next opening on the schedule?

 

SELECT 
    (SELECT MIN(DueDate + 1) 
     FROM Pin_Creation_Schedule 
     WHERE ServiceID = [@field:Pin_Creation_Details_ServiceID]
       AND DueDate >= CAST(GETDATE() AS DATE)
       AND NOT EXISTS (
           SELECT 1 
           FROM Pin_Creation_Schedule AS innerTable 
           WHERE innerTable.ServiceID = [@field:Pin_Creation_Details_ServiceID] 
             AND innerTable.DueDate = Pin_Creation_Schedule.DueDate + 1
       )
    ) AS NextGapDate
FROM Pin_Creation_Schedule
WHERE ServiceID = [@field:Pin_Creation_Details_ServiceID]
  AND DueDate >= CAST(GETDATE() AS DATE)

 

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Just replying in case it helps anyone else.  I was finally able to get this to work with the following SQL query in a calculated field.  There were several standard queries that weren't supported, but the following code works!

 

SELECT MIN(DATEADD(day, 1, DueDate)) 
FROM Pin_Creation_Schedule outerTable
WHERE ServiceID = [@field:Pin_Creation_Details_ServiceID]
  AND DueDate >= CAST(GETDATE() AS DATE)
  AND NOT EXISTS (
    SELECT 1 
    FROM Pin_Creation_Schedule innerTable 
    WHERE innerTable.ServiceID = outerTable.ServiceID 
      AND innerTable.DueDate = DATEADD(day, 1, outerTable.DueDate)
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...