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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:08:20+00:00 2026-05-18T08:08:20+00:00

I have 2 tables. for this example I will use only one users records.

  • 0

I have 2 tables. for this example I will use only one users records.
The first table has the user name and an evaluation date as such:

USER  EVALDATE
--------------
bobr  6/7/2010
bobr  9/20/2010
bobr  9/21/2010

The above table needs to be joined against this user history table, which has the history of the ID’s and the dates they were valid, to look for a match (the NULL date means current):

USER   STARTDATE   ENDDATE
----------------------------
bobr   2/20/2006   4/18/2010
bobr2  4/19/2010   9/7/2010
bobr   9/8/2010    null

What I’m trying to do in SQL Server 2005 is take the first record from the first table, loop it through the second table and when(if) the EVALDATE is within one of these date ranges and the IDs match, then flag that record from the first table as valid.

The current code takes the record from the first table and runs against all rows of the second table and kicks out a record for each invalid evaldate, so it kicks out a record when joined against the second table because the evaldate is not between the dates of the first record on the history table, even though the record is fine because the evaldate is between the start and end dates of the third record in the history table.

I hope this makes sense! In something like SAS I can create an array and loop through checking against each record in the history table. How do I do this in SQL? What I was trying to do was just update the first table with a flag if the records dates are invalid. Any ideas? Thanks!!!

  • 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-18T08:08:21+00:00Added an answer on May 18, 2026 at 8:08 am

    Try this:

    SELECT  [USER]
           ,[EVALDATE]
           ,CASE WHEN ( SELECT  COUNT(*)
                        FROM    [UserStartEndDates] b
                        WHERE   [a].[USER] = [b].[User]
                                AND [EVALDATE] BETWEEN [STARTDATE]
                                                   AND COALESE([ENDDATE],[EVALDATE])
                      ) > 0 THEN 1
                 ELSE 0
            END AS [IsValid]
    FROM    [Evaluations] a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a system table 'categories' with 2 fixed records. The user will
I have a table similar to this: CREATE TABLE example ( id integer primary
I have a data tables with this type of setup. Table A AID AName
I have three tables. This query will write down the right answer (x-lines for
I have 2 tables: posts tags Tags table is structured like this: post_id tag
I have 2 tables which in simplified form look like this: Products( id: int,
I have two tables (well, two relevant for this question) : Bets (holds the
I have three tables: page, attachment, page-attachment I have data like this: page ID
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
This issue is driving me mad. I have several tables defined, and CRUD stored

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.