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 847421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:46:51+00:00 2026-05-15T06:46:51+00:00

I have been working on a query that will return a suggested start date

  • 0

I have been working on a query that will return a suggested start date for a manufacturing line based on due date and the number of minutes needed to complete the task.

There is a calendar table(LINE_ID, CALENDAR_DATE, SCHEDULED_MINUTES) that displays per manufacturing line, the number of minutes scheduled for that day.

Example: (Usually 3 shifts worth of time scheduled per day, no weekends but can vary)

1, 06/8/2010 00:00:00.000, 1440
1, 06/7/2010 00:00:00.000, 1440
1, 06/6/2010 00:00:00.000, 0
1, 06/5/2010 00:00:00.000, 0
1, 06/4/2010 00:00:00.000, 1440

In order to get the suggested start date, I need to start with the due date and iterate downward through the days until i have accumulated enough time to complete the task.

My Question can something like this be done with CTE, or is this something that should be handled by a cursor. Or… am i just going about this the wrong way completely??

  • 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-15T06:46:52+00:00Added an answer on May 15, 2026 at 6:46 am

    would something like this work?

    ;WITH CALENDAR_WITH_INDEX(CALENDAR_DATE, AVAILABLE_MINUTES, DATE_INDEX)
    (
         SELECT
              CALENDAR_DATE,
              1440 - SCHEDULED_MINUTES,               /* convert scheduled minutes to available minutes */
              ROW_NUMBER() OVER (ORDER BY CALENDAR_DATE DESC) /* get day indexes. can't use DATE functions to get previous day (think holidays) */
         FROM
              CALENDAR
         WHERE
              LINE_ID = @LINE_ID AND
              CALENDAR_DATE < @DUEDATE                        /* use <= instead of < if you can do stuff on the scheduled date too */
    ),
    WITH TIME_SLICES (SCHEDULED_DATE, MINUTESPENDING, SLICE_INDEX)
    (
         SELECT 
              CALENDAR_DATE, 
              @DURATION - (AVAILABLE_MINUTES),                /* knocks of minutes available from our running total */
              DATE_INDEX
         FROM 
              CALENDAR_WITH_INDEX                             
         WHERE
              DATE_INDEX = 1                                  /* gets the first date usable date */
    
         UNION ALL
    
         SELECT 
              CALENDAR_DATE, 
              MINUTESPENDING - AVAILABLE_MINUTES
              DATE_INDEX
         FROM 
              CALENDAR_WITH_INDEX
              INNER JOIN TIME_SLICES 
                   ON DATE_INDEX = SLICE_INDEX + 1            /* this gets us the date 1 day before */
         WHERE
              MINUTESPENDING > 0                              /* stop when we have no more minutes */
    )
    SELECT MIN(SCHEDULED_DATE) FROM TIME_SLICES
    

    I guess performance is going to be bad because of the row_number, recursive part.

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

Sidebar

Related Questions

I've got a rather lengthy query i have been working with that is throwing
i have been working on query which uses compute by clause for avg and
I am having trouble writing a query. I have been working with UNION on
I've been working on some solutions for some questions that have been recently posted
I have been working on creating a kpi that compares the rank of a
I have a complicated query I've been working on, and I recently changed one
I have not been working with Monotouch (or the iphone for that matter) for
BACKGROUND I have been working on a tool that is supposed to work in
I'm trying to create a custom query that will show the number of stories
Have been working on this question for a couple hours and have come close

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.