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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:56:37+00:00 2026-05-21T05:56:37+00:00

It seems like this title has been used many times before, unfortunately I don’t

  • 0

It seems like this title has been used many times before, unfortunately I don’t know how else to describe my problem. So, first off, if you have a suggestion for a better title that will help future searchers, fire away!

Anyway, my problem is trying to write the SQL to return a resultset based on a set of rules applied to the following schema:

TABLE: Tests
COLUMNS: ID (PK), Name

TABLE: TestVersions    
COLUMNS: TestID (PK), Version (PK), IsActive

TABLE: TestSessions
COLUMNS: TestID (PK), TestVersion (PK), UserID (PK), Iteration (PK), Completed, CompletionDate

There is a relationship between [Tests] and [TestVersions] on [Tests].[ID] = [TestVersions].[TestID]. There is also a relationship between [TestSessions] and [TestVersions] on [TestSessions].[TestID] = [TestVersions].[TestID] AND [TestSessions].[TestVersion] = [TestVersions].[Version]

The result set should return [Tests].[ID], [Tests].[Name] and [TestVersions].[Version] based on the following rules:

  1. Any Test that has an associated record in TestSessions where Complete is false.
     
  2. The maximum Version for any Test that has no associated records in TestSessions but IsActive is true.
     
  3. This one is the complicated one. If a Test has associated records in TestSessions where all are completed (Complete is true) and has at least one associated record in TestVersions with IsActive true, I need to verify that the most recent CompletionDate is at least 30 days ago and, if so, return the highest Version from TestVersions.
     

Hopefully this makes sense.

  • 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-21T05:56:38+00:00Added an answer on May 21, 2026 at 5:56 am

    Here’s a quick script that creates temp tables, fills them with data, and shows you how i’d approach the problem you’re having. Let me say up front that this kind of stuff is sometimes best left to the business logic layers. Also, I’m not sure that your description is very accurate because there seem to be some inconsistencies. With that said, I hope you can take away how to approach your problem, break it down into parts and solve it. Don’t worry about optimizing up front, just write clean and logical code. Optimizer will tell you if you’re making any mistakes. This assumes SQL Server 2008:

    declare @Tests table
    (
        ID int,
        Name varchar(100)
    )
    
    declare @TestVersions table
    (
        TestID int,
        Version int,
        IsActive bit
    )
    
    declare @TestSessions table
    (
        TestID int,
        TestVersion int,
        UserID varchar(100),
        Iteration int,
        Completed bit,
        CompletionDate date
    )
    
    insert into @Tests
    select 1, 'one'     union all
    select 2, 'two'     union all
    select 3, 'three'   union all
    select 4, 'four'    union all
    select 5, 'five'
    
    insert into @TestVersions
    select 1, 1, 0      union all
    select 2, 1, 1      union all
    select 3, 1, 0      union all
    select 4, 1, 0      union all
    select 5, 1, 1      union all
    select 1, 2, 0      union all
    select 2, 2, 0      union all
    select 3, 2, 1      union all
    select 4, 2, 0      union all
    select 1, 3, 1      union all
    select 2, 3, 1
    
    insert into @TestSessions
    select 1, 1, 'a', 1, 1, GETDATE()-101       union all
    select 2, 1, 'b', 1, 1, GETDATE()-11        union all
    select 3, 1, 'c', 1, 0, null                union all
    select 4, 1, 'd', 1, 1, GETDATE()-103       union all
    select 5, 1, 'e', 1, 1, GETDATE()-101       union all
    select 1, 2, 'f', 1, 1, GETDATE()-15        union all
    select 2, 2, 'g', 1, 0, null                union all
    select 3, 2, 'h', 1, 1, GETDATE()-17        union all
    select 4, 2, 'i', 1, 0, null                union all
    select 1, 3, 'j', 2, 1, GETDATE()-109       union all
    select 2, 3, 'k', 2, 1, GETDATE()-101       union all
    select 2, 1, 'l', 3, 1, GETDATE()-120       union all
    select 3, 1, 'm', 1, 1, GETDATE()-11        union all
    select 4, 1, 'n', 1, 1, GETDATE()-140       union all
    select 5, 1, 'a', 1, 0, null                union all
    select 1, 2, 'b', 1, 1, GETDATE()-160       union all
    select 2, 2, 'c', 2, 0, null                union all
    select 3, 2, 'd', 1, 1, GETDATE()-17        union all
    select 4, 2, 'e', 1, 0, null                union all
    select 1, 3, 'f', 2, 1, GETDATE()-4         union all
    select 2, 3, 'g', 3, 1, GETDATE()-101
    
    
     select Id
            ,Name
            ,Version
       from @Tests t
                inner join
            @TestVersions v on t.ID = v.TestID
    
      -- any test with a session with completed = false
      where exists
            (select *
               from @TestSessions s1
              where s1.Completed = 0
                and s1.TestID = t.ID
                and s1.TestVersion = v.Version
            )
         or (
      -- any max version of a test which has no sessions
                not exists
                (select *
                   from @TestSessions s1
                  where s1.TestID = t.ID
                    and s1.TestVersion = v.Version 
                )
                and v.Version =
                (select MAX(Version)
                   from @TestVersions v2
                  where v2.TestID = t.ID
                )
            )
         or (
         -- no uncompleted sessions
                not exists
                (select *
                   from @TestSessions s1
                  where s1.Completed = 0
                    and s1.TestID = t.ID
                    and s1.TestVersion = v.Version
                )
         -- we're already inner joining to versions, so we just need to check IsActive
                and IsActive = 1
         -- the most recent completion date is at least 30 days ago (not quite 30 days ago but i'm lazy)
                and GETDATE() - 30 <=
                (select MAX(CompletionDate)
                   from @TestSessions s2
                  where s2.TestID = t.ID
                    and s2.TestVersion = v.Version
                )
         -- not sure what you mean by "return", but I'm sure you can figure out how to project what you need
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the tagging stuff has been mentioned many times, but I can't seem
I know this question has been asked multiple times (however, I could still not
I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
Like the title says, if anyone has the answer I would like to know.
This seems like it should be really easy but I've been having trouble with
I know this is something that has been discussed over and over, and I
This question has been asked before - but with no satisfying answer at all!
Apologies if this question has been asked before. I'm hoping that someone can step
Seems like this should be obvious, but how do I send arrow key presses
Seems like this should be simple, but powershell is winning another battle with me.

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.