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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:46:17+00:00 2026-05-20T15:46:17+00:00

I have two tables, a header and a detail table: header id name detail

  • 0

I have two tables, a header and a detail table:

header
id
name

detail
headerID
dueDate
extensionDate

I’m trying to make a query that will pull the ID and Name of a header who’s detail dueDate is 5 days from now (if extensionDate is null), or if there is an extensionDate, pull the header who has an extensionDate that is 5 days from now. Can this be done in a single query? I hope this makes sense.
I’m having trouble with determining if there is an extensionDate, and checking that, or if not, check the original dueDate. Thank you for your help!

  • 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-20T15:46:18+00:00Added an answer on May 20, 2026 at 3:46 pm
    select id, name
    from header
    where exists (
        select *
        from detail
        where headerid=header.id
        and (extensiondate = DATEDIFF(d, -5, getdate())
         or (duedate = DATEDIFF(d, -5, getdate()) and extensiondate is null)))
    

    Notes:

    1. DATEDIFF(d, -5, getdate() gives you a date exactly 5 days in the future, without the time portion.
    2. If you had indexes on extensiondate and duedate, this can utilise them separately. Compare this with the other queries presented here which will perform a table scan.

    EDIT (also contains alternative query)

    Why EXISTS? First create these test tables. About 8% of details will match the conditions, which represents 5% of header records (unique).

    create table header (id int, name varchar(max))
    insert header select distinct number, replicate(number, 10) from master..spt_values
    create table detail (id int identity primary key, headerid int, duedate datetime, extensiondate datetime)
    create index ix_duedate on detail(duedate)
    create index ix_extensiondate on detail(extensiondate)
    insert detail (headerid, duedate, extensiondate)
    select number,
        DATEDIFF(d,-convert(bigint,convert(varbinary(max),NEWID()))%20 - 10, GETDATE()),
        case when -convert(bigint,convert(varbinary(max),NEWID()))%2 = 1 then null else
        DATEDIFF(d,-convert(bigint,convert(varbinary(max),NEWID()))%20 - 10, GETDATE()) end
    from master..spt_values
    

    Check the execution plans for the following two, using Ctrl-M

    select id, name
    from header
    where exists (
        select *
        from detail
        where headerid=header.id
        and (extensiondate = DATEDIFF(d, -5, getdate())
         or (duedate = DATEDIFF(d, -5, getdate()) and extensiondate is null)))
    
    select distinct h.id, h.name
    from header h
    join detail d on d.headerid = h.id
      and (d.extensiondate = DATEDIFF(d, -5, getdate())
         or (d.duedate = DATEDIFF(d, -5, getdate()) and d.extensiondate is null))
    

    The first one is just slightly faster

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

Sidebar

Related Questions

No related questions found

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.