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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:04:51+00:00 2026-06-10T02:04:51+00:00

I have a table that stores car prices with a structure like: +—————–+————–+——+—–+———+—————-+ |

  • 0

I have a table that stores car prices with a structure like:

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| model           | varchar(255) | YES  |     | NULL    |                |
| make            | varchar(255) | YES  |     | NULL    |                |
| year            | varchar(255) | YES  |     | NULL    |                |
| avg_price       | decimal(8,2) | YES  |     | NULL    |                |
| median_price    | decimal(8,2) | YES  |     | NULL    |                |
| created_at      | datetime     | YES  |     | NULL    |                |
| updated_at      | datetime     | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

The data for a given year might be inserted at different times.

For example model 'Honda', make 'Accord' might have results like:
+------+-----------+--------------+--------+---------------------+
| year | avg_price | median_price | model  | created_at          |
+------+-----------+--------------+--------+---------------------+
| 1992 |   2431.29 |      2000.00 | accord | 2012-02-23 17:31:41 |
| 1993 |   2609.13 |      2195.00 | accord | 2012-02-23 17:31:44 |
| 1994 |   2858.81 |      2400.00 | accord | 2012-02-23 17:31:44 |
| 2000 |   4771.99 |      4450.00 | accord | 2012-02-23 17:31:46 |
| 2001 |   5260.16 |      5000.00 | accord | 2012-02-23 17:31:46 |
| 2000 |   4860.19 |      4795.00 | accord | 2012-08-15 06:09:52 |
| 2001 |   5071.49 |      4990.00 | accord | 2012-08-15 06:09:52 |
| 2002 |   5872.80 |      5795.00 | accord | 2012-08-15 06:09:52 |
| 2003 |   7521.44 |      7950.00 | accord | 2012-08-15 06:09:52 |
| 2004 |   8348.19 |      8495.00 | accord | 2012-08-15 06:09:52 |

I would like to retrieve all honda accord data that is the latest for that year of car make.

so in the above example, I would like to retrieve data from 2012-08-15 06:09:52 for years 2000,2001,2002,2003,2004
but the older years would be from the date 2012-02-23 17:31:41

select year,avg_price,median_price,model,created_at 
from car_prices 
where make='honda' and    model= 'accord' group by year asc

The above query gets distinct data for each year but not the last record inserted for each year.

Any ideas how to get distinct data for each year as well as the latest?

  • 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-10T02:04:52+00:00Added an answer on June 10, 2026 at 2:04 am

    For both performance and guarnanteed behaviour, you should create a lookup and join on that.

    In your case you want to find the most recent created_at value for any given make, model, year group. The sub-query in the code below does that.

    Then you join that back on your original data again, finding only the records that have those make, model, year, created_at values.

    This does mean that it you have more than one record with the same make, model, year, created_at values, you will get multiple results for that make, model, year.

    Ensure that you have an index covering (make, model, year, created_at) to make the search for the most recent created_at quick, as well as the join.

    SELECT
      *
    FROM
      car_prices
    INNER JOIN
    (
      SELECT
        make, model, year, MAX(created_at) as created_at
      FROM
        car_prices
      GROUP BY
        make, model, year
    )
      AS most_recent
        ON  car_prices.make       = most_recent.make
        AND car_prices.model      = most_recent.model
        AND car_prices.year       = most_recent.year
        AND car_prices.created_at = most_recent.created_at
    WHERE
          most_recent.make  = 'honda'
      AND most_recent.model = 'accord'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that stores a tree like structure of file names. There
I have a database table that stores document information (DocID, Name, Type, Owner, etc).
I have a table that stores photo information with id as Primary key: id(PK),
I have a table that stores records like this: name | stat | value
I have a table that has a foreign key to a table that stores
I have a table that stores the price of various products and would like
I have a database table that stores imported information. For simplicity, its something like:
I have a table that stores various types of flags. Each flag type has
I have a table that stores data that has been entered regarding the amount
I have a table that stores email in 3 diffrent columns name host and

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.