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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:04:13+00:00 2026-06-17T18:04:13+00:00

(SQL SERVER 2008) I have timestamped records in multiple tables to join to a

  • 0

(SQL SERVER 2008)
I have timestamped records in multiple tables to join to a master/base table. The timepoints are sometimes equal to the base table, but sometimes not.

Code to create tables:

create table base (time float);
create table table2 (time float, val2 char(1));
create table table3 (time float, val3 int);
insert into base values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
insert into table2 values (1, 'a'),(5, 'z'),(6, 'm'),(9, 'b');
insert into table3 values (1.5, 1),(5.3, 10),(5.5, 0),(8.1, 4);

The result set should be one row per record from the base table and the “most recent” value from the other tables. Previously, these tables were “joined” in Excel using Vlookup set to TRUE, which takes the closest-without-going-over match from a sorted table.

Final result should look like:

time | val2 | val3
1  | a | NULL
2  | a | 1
3  | a | 1
4  | a | 1
5  | z | 1
6  | m | 0
7  | m | 0
8  | m | 0
9  | b | 4
10 | b | 4

How can I replicate this with a SQL statement?

I’ll take readability over efficiency here since there are only ~100 records at play.

  • 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-06-17T18:04:15+00:00Added an answer on June 17, 2026 at 6:04 pm

    Perhaps the most readable are correlated subqueries in the select clause:

    I’m not generally a fan of select within select, but the correlated subquery does mimic the behavior of the Excel vlookup.

    Select
      b.time,
      (Select max(t2.val2) From table2 t2 Where b.time >= t2.time) As val2,
      (Select max(t3.val3) From table3 t3 Where b.time >= t3.time) As val3
    From
      base b
    Order By
      b.time;    
    

    http://sqlfiddle.com/#!3/3545e/18

    (Thank you Laurence for the above code and the SQL Fiddle.)

    The use of max requires that the values be non-decreasing. The following version works regardless:

    select
      b.time,
      (select top 1 t2.val2 from table2 t2 where b.time >= t2.time order by t2.time Desc) as val2,
      (select top 1 t3.val3 from table3 t3 where b.time >= t3.time order by t3.time Desc) as val3
    from
      base b
    

    http://sqlfiddle.com/#!6/a5148/5

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

Sidebar

Related Questions

In SQL Server 2008 I have two tables. One table for users: id_user -
In SQL Server 2008 I have a user table like the following: Userid Username
In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
In SQL Server 2008: I have one table, and I want to do something
I use SQL Server 2008 and have a table with 5 char typed columns.
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
I am using T/SQL in SQL Server 2008 I have a table MyTable with
Would it be possible in SQL Server 2008 to have a table created with
Microsoft SQL Server 2008 I have two tables. One has a column in it
In Sql Server 2008 I have a DB table, and the data includes a

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.