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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:51:10+00:00 2026-05-13T13:51:10+00:00

Normally I would just do this in the code itself, but I am curious

  • 0

Normally I would just do this in the code itself, but I am curious if this can be accomplished efficiently in TSQL.

Table 1 
Date - Value
Table 2
Date - Discount

Table 1 contains entries for each day. Table 2 contains entries only when the discount changes. A discount applied to a value is considered valid until a new discount is entered.

Example data:

Table 1  
1/26/2010 - 10  
1/25/2010 - 9  
1/24/2010 - 8  
1/24/2010 - 9   
1/23/2010 - 7    
1/22/2010 - 10  
1/21/2010 - 11
Table 2
1/26/2010 - 2  
1/23/2010 - 1  
1/20/2010 - 0  

What I need returned is the following: T1 Date - T1 Value - T2 Discount

Example data:

1/26/2010 - 10 - 2    
1/25/2010 - 9  - 1  
1/24/2010 - 8  - 1  
1/24/2010 - 9  - 1  
1/23/2010 - 7  - 1    
1/22/2010 - 10 - 0  
1/21/2010 - 11 - 0  

Possible or am I better off just continuing to do this in the code?

  • 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-13T13:51:10+00:00Added an answer on May 13, 2026 at 1:51 pm

    I believe this subquery will do it (not tested).

    select *, 
       (select top 1 Discount 
        from table2 
        where table2.Date <= t.Date 
        order by table2.Date desc) as Discount
    from Table1 t
    

    Perhaps not the most performant however.

    Edit:

    Test code:

    create table #table1 ([date] datetime, val int)
    create table #table2 ([date] datetime, discount int)
    
    insert into #table1 ([date], val) values ('1/26/2010', 10)
    insert into #table1 ([date], val) values ('1/25/2010', 9)
    insert into #table1 ([date], val) values ('1/24/2010', 8)
    insert into #table1 ([date], val) values ('1/24/2010', 9)
    insert into #table1 ([date], val) values ('1/23/2010', 7)
    insert into #table1 ([date], val) values ('1/22/2010', 10)
    insert into #table1 ([date], val) values ('1/21/2010', 11)
    
    insert into #table2 ([date], discount) values ('1/26/2010', 2)
    insert into #table2 ([date], discount) values ('1/23/2010', 1)
    insert into #table2 ([date], discount) values ('1/20/2010', 0)
    
    select *, 
       (select top 1 discount 
        from #table2 
        where #table2.[date] <= t.[date]
        order by #table2.[date] desc) as discount
    from #table1 t
    
    drop table #table1
    drop table #table2
    

    Results:

    2010-01-26 00:00:00.000 10  2
    2010-01-25 00:00:00.000 9   1
    2010-01-24 00:00:00.000 8   1
    2010-01-24 00:00:00.000 9   1
    2010-01-23 00:00:00.000 7   1
    2010-01-22 00:00:00.000 10  0
    2010-01-21 00:00:00.000 11  0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.