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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:39:41+00:00 2026-06-17T04:39:41+00:00

I have a table, it looks something like this: id year effective price 1

  • 0

I have a table, it looks something like this:

id  year    effective   price
1   2000    2012-01-01  1000
2   2001    2012-01-01  1100
3   2002    2012-01-01  1200
4   2003    2012-01-01  1300
5   2000    2012-03-01  1500
6   2001    2012-03-01  1600
7   2002    2012-03-01  1700
8   2003    2012-03-01  1800
9   2000    2011-12-01  900
10  2001    2011-12-01  1000
11  2002    2011-12-01  1100
12  2003    2011-12-01  1200

In this table is a bunch of data that contains price histories for a number of items. They are not in any manner of regular order, and it seems that people have randomly gone into the database and made changes. I did not set this database up, nor have I developed the application that inserts and updates the data. My only objective is to present the data specified in the manner my client wants.

That being said, I am having a difficult time getting the information I need. In the above example data, I need to be able to sort the prices and get a distinct number for each specific year. This has to be filtered by the most recent effective date as input by the end user.

For instance, if the user wanted the most recent data (as of today’s date), the response should be something like this:

5   2000    2012-03-01  1500
6   2001    2012-03-01  1600
7   2002    2012-03-01  1700
8   2003    2012-03-01  1800

If the user, on the other hand, wanted the most recent data as of 2012-02-01, then it should return:

1   2000    2012-01-01  1000
2   2001    2012-01-01  1100
3   2002    2012-01-01  1200
4   2003    2012-01-01  1300

My issue lies in not knowing how to build this query. Is it even possible to build this as a single query, or will I have to perform multiple queries in order to get the data? I’d like as small of a footprint as possible, since I have to grab data from four different tables which share years, but have other different data.

I have tried this query to no effect:

select id, year, effective, price
from mytable
group by year
having effective < '2012-02-01'

which gives this result:

1   2000    2012-01-01  1000
2   2001    2012-01-01  1100
3   2002    2012-01-01  1200
4   2003    2012-01-01  1300

I would have thought that this would have given me the right information, as it seems logical.

I tried this query, also to no effect:

select distinct year, id, effective, price
from mytable
group by effective
having effective < '2012-02-01'

which returns this result:

1   2000    2012-01-01  1000
5   2000    2012-03-01  1500
9   2000    2011-12-01  900

This query seems to be completely wrong.

Any suggestions on how to get this to perform the query correctly?

  • 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-17T04:39:42+00:00Added an answer on June 17, 2026 at 4:39 am

    Using a correlated subquery to get the MAX(effective) per year and joining against the main table will work, if you filter the date inside the subquery. The idea is that the subquery will return the set of model years and effective date which are the maximum date less than what you specify in the WHERE clause. Those returned columns can be joined back to the main table to pull in the rest of the columns.

    SELECT 
      mytable.*
    FROM
      mytable
      JOIN (
         /* Join on a subquery which returns the greatest date per year */
         SELECT MAX(effective) as effective, year
         FROM mytable 
         /* Filtering on some date in the subquery WHERE clause... */
         WHERE effective < '2012-01-01'
         GROUP BY year
      /* Since the subquery returns effective,year, join on both those cols against the main table */
      ) maxd ON mytable.effective = maxd.effective AND mytable.year = maxd.year
    

    Example effective 2012-01-01: http://sqlfiddle.com/#!2/05546/4

    Example effective 2013-01-01: http://sqlfiddle.com/#!2/05546/5

    • 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 looks something like this: Name Year Value A 2000
I have a table that looks something like this Employee Date Sean 21-07-2012 Peter
I have a table that looks something like this: id | firstperson | secondperson
I have a relationships table, the table looks something like this ------------------------ | client_id
I have a MySQL table that looks something like this: +-----+------------+ | id |
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have two tables which looks something like this Table Queue int ID; string
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a table that looks something like this: +----------------------+-------------------+ | This text is

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.