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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:04:13+00:00 2026-05-27T09:04:13+00:00

I got this table A: | id | date | =================== | 1 |

  • 0

I got this table “A”:

| id | date       |
===================
| 1  | 2010-01-13 |
| 2  | 2011-04-19 |
| 3  | 2011-05-07 |
| .. | ...        |

and this table “B”:

| date       | value |
======================
| 2009-03-29 | 0.5   |
| 2010-01-30 | 0.55  |
| 2011-08-12 | 0.67  |

Now I am looking for a way to JOIN those two tables having the “value” column in “B” mapped to the dates in “A”. The tricky part for me here is that table “B” only stores the change date and the new value. Now when I need this value in table “A” the SQL needs to look back what date is the next below the date it is asking the value for.

So in the end the JOIN of those tables should look like this:

| id | date       | value |
===========================
| 1  | 2010-01-13 | 0.5   |
| 2  | 2011-04-19 | 0.55  |
| 3  | 2011-05-07 | 0.55  |
| .. | ...        | ...   |

How can I do this?

  • 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-27T09:04:14+00:00Added an answer on May 27, 2026 at 9:04 am
    -- Create and fill first table
    CREATE TABLE `id_date` (
      `id` int(11) NOT NULL auto_increment,
      `iddate` date NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    
    INSERT INTO `id_date` VALUES(1, '2010-01-13');
    INSERT INTO `id_date` VALUES(2, '2011-04-19');
    INSERT INTO `id_date` VALUES(3, '2011-05-07');
    
    -- Create and fill second table    
    CREATE TABLE `date_val` (
      `mydate` date NOT NULL,
      `myval` varchar(4) collate utf8_bin NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    
    INSERT INTO `date_val` VALUES('2009-03-29', '0.5');
    INSERT INTO `date_val` VALUES('2010-01-30', '0.55');
    INSERT INTO `date_val` VALUES('2011-08-12', '0.67');
    
    -- Get the result table as asked in question
    SELECT iddate, t2.mydate, t2.myval
    FROM `id_date` t1
    JOIN date_val t2 ON t2.mydate <= t1.iddate
    AND t2.mydate = ( 
    SELECT MAX( t3.mydate ) 
    FROM `date_val` t3
    WHERE t3.mydate <= t1.iddate )
    

    What we’re doing:

    • for each date in the id_date table (your table A),
    • we find the date in the date_val table (your table B)
    • which is the highest date in the date_val table (but still smaller than the id_date.date)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got tables like this: Table Articles --------------- AID(PK) Name PublisherID Words Table Books
I got a table like this: Id Date1 Date2 Status ---------------------------------------------- 1 01/01/2010 null
I have two dates of the form: Start Date: 2010-12-24 End Date: 2011-10-06 (today)
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
I've got this table with an int(11) column and hundreds of millions of rows.
So I've got this audit table, looks like this: USE [DatabaseName] GO /****** Object:
I tried this: ALTER TABLE My.Table DROP MyField and got this error: -MyField is
I've got a situation like this: Table: FunTable ItemKey ItemName ItemPriceEffectiveDate ItemPrice 11 ItemA
I've got this bit of JQUERY: <script> function initNav() { $('.nav tr table td
I've got m2m relationship like this: #main table CREATE TABLE products_product ( id integer

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.