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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:17:10+00:00 2026-05-10T23:17:10+00:00

How can you join between a table with a sparse number of dates and

  • 0

How can you join between a table with a sparse number of dates and another table with an exhaustive number of dates such that the gaps between the sparse dates take the values of the previous sparse date?

Illustrative example:

PRICE table (sparse dates): date        itemid  price 2008-12-04  1       $1 2008-12-11  1       $3 2008-12-15  1       $7   VOLUME table (exhaustive dates): date         itemid  volume_amt 2008-12-04   1       12345 2008-12-05   1       23456 2008-12-08   1       34567 2008-12-09   1       ... 2008-12-10   1 2008-12-11   1 2008-12-12   1 2008-12-15   1 2008-12-16   1 2008-12-17   1 2008-12-18   1 

Desired result:

date       price  volume_amt 2008-12-04 $1     12345 2008-12-05 $1     23456 2008-12-08 $1     34567 2008-12-09 $1     ... 2008-12-10 $1 2008-12-11 $3 2008-12-12 $3 2008-12-15 $7 2008-12-16 $7 2008-12-17 $7 2008-12-18 $7 

Update:

A couple people have suggested a correlated subquery that accomplishes the desired result. (Correlated subquery = a subquery that contains a reference to the outer query.)

This will work; however, I should have noted that the platform I’m using is MySQL, for which correlated subqueries are poorly optimized. Any way to do it without using a correlated subquery?

  • 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. 2026-05-10T23:17:11+00:00Added an answer on May 10, 2026 at 11:17 pm

    This isn’t as simple as a single LEFT OUTER JOIN to the sparse table, because you want the NULLs left by the outer join to be filled with the most recent price.

    EXPLAIN SELECT v.`date`, v.volume_amt, p1.item_id, p1.price FROM Volume v JOIN Price p1   ON (v.`date` >= p1.`date` AND v.item_id = p1.item_id) LEFT OUTER JOIN Price p2   ON (v.`date` >= p2.`date` AND v.item_id = p2.item_id     AND p1.`date` < p2.`date`) WHERE p2.item_id IS NULL; 

    This query matches Volume to all rows in Price that are earlier, and then uses another join to make sure we find only the most recent price.

    I tested this on MySQL 5.0.51. It uses neither correlated subqueries nor group by.

    edit: Updated the query to match to item_id as well as date. This seems to work too. I created an index on (date) and an index on (date, item_id) and the EXPLAIN plan was identical. An index on (item_id, date) may be better in this case. Here’s the EXPLAIN output for that:

    +----+-------------+-------+------+---------------+---------+---------+-----------------+------+--------------------------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref             | rows | Extra                                | +----+-------------+-------+------+---------------+---------+---------+-----------------+------+--------------------------------------+ |  1 | SIMPLE      | p1    | ALL  | item_id       | NULL    | NULL    | NULL            |    6 |                                      |  |  1 | SIMPLE      | v     | ref  | item_id       | item_id | 22      | test.p1.item_id |    3 | Using where                          |  |  1 | SIMPLE      | p2    | ref  | item_id       | item_id | 22      | test.v.item_id  |    1 | Using where; Using index; Not exists |  +----+-------------+-------+------+---------------+---------+---------+-----------------+------+--------------------------------------+ 

    But I have a very small data set, and the optimization may depend on larger data sets. You should experiment, analyzing the optimization using a larger data set.

    edit: I pasted the wrong EXPLAIN output before. The one above is corrected, and shows better use of the (item_id, date) index.

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

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SET collation_connection = 'utf8_general_ci'; then for your databases ALTER DATABASE… May 11, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer All the Text.Regex.* modules make heavy use of typeclasses, which… May 11, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer main can wrap the execfile into a try/except block: sys.exit… May 11, 2026 at 9:22 pm

Related Questions

Is there any way for a DBA to peek in on the execution plan
I have an app where I want to link an instance of a model
Im writing a little python program that goes through an XML file and does
I have a challenge I need some input on. I am currently recruiting programmers

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.