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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:48:50+00:00 2026-05-23T23:48:50+00:00

I have a table code_prices that looks something like this: CODE | DATE |

  • 0

I have a table code_prices that looks something like this:

CODE   |   DATE     | PRICE
ABC    | 25-7-2011  |  2.81
ABC    | 23-7-2011  |  2.52
ABC    | 22-7-2011  |  2.53
ABC    | 21-7-2011  |  2.54
ABC    | 20-7-2011  |  2.58
ABC    | 17-7-2011  |  2.42
ABC    | 16-7-2011  |  2.38

The problem with the data set is there are gaps in the dates, so I may want to look for the price of item ABC on the 18th however there is no entry because the item wasnt sold on this date. So I would like to return the most recent hisotrical entry for the price.

Say if I query on the date 19-7-2011, I would like to return the entry on the 17th then the next 10 avalaible entries.

If however I query for the price of ABC on the 20th, I would want to return the price on the 20th and the next 10 prices after that…

What is the most efficient way to go about this either in SQL statement or using a stored proc.

I can think of just writing a stored proc which takes the date as a param and then querying for all rows where DATE >= QUERY-DATE ordering by the date and then selecting the 11 items (via limit). Then basically I need to see if that set contains the current date, if it does then return, otherwise I will need to return the 10 most recent entires out of those 11 and also do another query on the table to return the previous entry by getting the max date where date < QUERY-DATE. I am thinking there might be a better way, however I’m not an expert with SQL (clearly)…

Thanks!

  • 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-23T23:48:51+00:00Added an answer on May 23, 2026 at 11:48 pm

    This is for one specific code:

    SELECT code, `date`, price
    FROM code_prices
    WHERE code = @inputCode
      AND `date` >=
            ( SELECT MAX(`date`)
              FROM code_prices
              WHERE code = @inputCode
                AND `date` <= @inputDate
            )
    ORDER BY `date`
    LIMIT 11
    

    For ABC and 19-7-2011, the above will you give the row for 17-7-2011 and the 10 subsequent rows (20-7-2011, 21-7-2011, etc)

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

Sidebar

Related Questions

I have a table with a code like this: <table cellpadding=6 rules=groups frame=no> <thead>
I have a HTML file that has code similar to the following. <table> <tr>
Situation looks like this: We have product 'A123' and we have to remember lowest
Thanks for looking on this problem. I have a page that is totally valid
I have a table defined (see code snippet below). How can I add a
I have have some code which adds new cells to a table and fills
I have the following code: $sql = INSERT INTO table VALUES ('', ...); $result
I have the following code, I'm trying to get a table with 4 columns
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have to following code: http://www.nomorepasting.com/getpaste.php?pasteid=22987 If PHPSESSID is not already in the table

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.