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

  • Home
  • SEARCH
  • 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 6933255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:48:33+00:00 2026-05-27T11:48:33+00:00

Ok, so this feels like a complicated query due to a poor design of

  • 0

Ok, so this feels like a complicated query due to a poor design of the database, but alas — it is there and in need of solving. The table has 5 fields: id(PK), itemNumber(FK), completedDate, completedBy(FK), nextDue.

In the application, when someone records a task as being done, it fills in the completedDate, calculates the next due date and puts that in nextDue.

What I need, is a query that will basically look at a record’s nextDue date, then look ahead to the next record that has the same itemNumber, and compare with that completedDate; then return the row if the completedDate exceeds the completedBy date. Does that make sense? Can this be done? It sounds like it would have to be done procedurally vs. pure sql. TIA

UDPATE:
I guess I should be more specific. I need to use this query in a report, so if it needs to be done in VBA, I’m not sure how to get the results into that.

enter image description here

  • 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-27T11:48:33+00:00Added an answer on May 27, 2026 at 11:48 am

    I find the best way to think about these weird queries is to break ’em down into smaller manageable queries in my mind. So, I think about this as first associating and then comparing two records: The ‘current’ and the ‘next’ records. The first thing we want to do is associate each record (which will become the ‘current’) with it’s corresponding ‘next’ record.

    So, first, a query that gets all records IDs and their corresponding “next record”. This uses a less than “<” in the JOIN statement to get all of the records that are after the current record.

    SELECT
         T.id as currentId,
         MIN(A.id) as nextId
    FROM
         rec_completionDate as T
              JOIN
         rec_completionDate as A ON T.itemNumber = A.itemNumber and T.id < A.id
    GROUP BY
         T.id
    

    Follow it up with a query that, using the above as a subquery, returns all of the appropriate fields for each record (current and next):

    SELECT
         current.*,
         next.*
    FROM
         ( ... [subquery goes here] ...) sq
              JOIN
         rec_completionDate as current ON sq.currentId = current.id
              JOIN
         rec_completionDate as next ON sq.nextId = next.id
    

    Lastly, we’ll want to limit the results of that last query with the following WHERE clause:

    WHERE
         next.completedDate > current.nextDue
    

    So, putting it all together:

    SELECT
         current.*,
         next.*
    FROM
         ( 
         SELECT
              T.id as currentId,
              MIN(A.id) as nextId
         FROM
              rec_completionDate as T
                   JOIN
              rec_completionDate as A ON T.itemNumber = A.itemNumber and T.id < A.id
         GROUP BY
              T.id
         ) sq
              JOIN
         rec_completionDate as current ON sq.currentId = current.id
              JOIN
         rec_completionDate as next ON sq.nextId = next.id
    WHERE
         next.completedDate > current.nextDue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It feels like there must be some semi-simple solution to this, but I just
This feels like a really basic question, but I can't figure it out anyway..
This feels like it should be pretty simple, but not much seems to be
This feels like the kind of code that only fails in-situ, but I will
OK, this feels like an idiot question, but I'm stuck - I don't know
I feel like this is a dumb question and I'm missing something, but I
I feel like this is something I should already know, but I'm just not
I feel like this is easy but I am missing something... Using jQuery, I
I feel like this should be a no brainer, but clearly I'm missing something...
For an access database that looks like this: (All text fields) Co1 Co2 Co3

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.