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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:30:56+00:00 2026-05-27T02:30:56+00:00

I have two tables that I am trying to join in a query, but

  • 0

I have two tables that I am trying to join in a query, but I’m having some difficulty getting the required output.

Table: Scans

ScanId  ScanTime
1        8:00
2        8:15
3        9:00
4        9:30
6       10:00
7       10:45
8       11:00
9       11:10

Table: Responses

ScanId  RespA  RespB
3       ABC    X
7       DEF    Y
9       GHI    Z

There is a foreign key on Responses.ScanId that references Scans.ScanId.

The Responses table may or may not have a corresponding row for each row in the Scans table.

What I need to do is produce the output shown below, each row in the Scans table is returned with the most recent previous response values.

For Scans.ScanId 1 and 2, there is no previous response so the Response columns are null.

For Scans.ScanId 3, 4 and 6, the most recent previous RespA is ABC and RespB is X (ScanId=3)

For Scans.ScanId 7 and 8, the most recent previous RespA is DEF and RespB is Y (ScanId=7)

For Scan.ScanId 9, the most recent previous RespA is GHI and RespB is Z (ScanId=9)

Desired Output:

ScanId  ScanTime  RespScanId  RespA  RespB
1        8:00     NULL        NULL   NULL
2        8:15     NULL        NULL   NULL
3        9:00     3           ABC    X
4        9:30     3           ABC    X
6       10:00     3           ABC    X
7       10:45     7           DEF    Y
8       11:00     7           DEF    Y
9       11:10     9           GHI    Z

I’m having difficulty figuring out how to write the join clause for this one. It needs to run on Sql Server 2005 and above.

  • 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-27T02:30:56+00:00Added an answer on May 27, 2026 at 2:30 am

    Here’s a solution using a CTE…

    DECLARE @Scans TABLE (
        ScanID INT,
        ScanTime DATETIME
    )
    
    DECLARE @Responses TABLE (
        ScanID INT, 
        RespA VARCHAR(50),
        RespB VARCHAR(50)
    )
    
    INSERT INTO @Scans VALUES (1, '8:00')
    INSERT INTO @Scans VALUES (2, '8:15')
    INSERT INTO @Scans VALUES (3, '9:00')
    INSERT INTO @Scans VALUES (4, '9:30')
    INSERT INTO @Scans VALUES (6, '10:00')
    INSERT INTO @Scans VALUES (7, '10:45')
    INSERT INTO @Scans VALUES (8, '11:00')
    INSERT INTO @Scans VALUES (9, '11:10')
    
    INSERT INTO @Responses VALUES (3, 'ABC', 'X')
    INSERT INTO @Responses VALUES (7, 'DEF', 'Y')
    INSERT INTO @Responses VALUES (9, 'GHI', 'Z')
    
    ;WITH ResponsesScan AS
    (
        SELECT
            r.*, s.ScanTime
        FROM
            @Responses r
            JOIN @Scans s ON s.ScanID = r.ScanID
    )
    SELECT
        s.ScanID,
        s.ScanTime,
        rs.ScanID AS RespScanId,
        rs.RespA,
        rs.RespB
    FROM
        @Scans s 
        LEFT JOIN ResponsesScan rs
            ON rs.ScanID = (
                SELECT TOP 1 ScanID
                FROM ResponsesScan
                WHERE ScanTime <= s.ScanTime
                ORDER BY ScanTime DESC
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use natural join to join two tables in mySQL that have
I'm trying to join two relatively simple tables together, but my query is experiencing
I have two tables that I am joining with the following query... select *
I have two tables that are related via a mapping table: keywords titles I
I'm trying to join two table (call them table1 and table2) but only return
I'm trying to join two SQL tables, parent (which I have complete design control
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I have two tables that are considered a single entity in my domain model.
Suppose I have two tables that are linked (one has a foreign key to

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.