?
Solved

MySQL maximum date before a given date

Posted on 2013-01-03
5
Medium Priority
?
209 Views
Last Modified: 2014-12-20
I have a table that has a series of dates along with an ID:
MY_ID    MY_DATE
A             3/6/1987 12:00:00 AM
A            4/1/1988 12:00:00 AM
A            4/28/1989 12:00:00 AM
B            3/6/1987 12:00:00 AM
B           4/1/1988 12:00:00 AM

I would like to find the maximum A that is before say 04/27/1988.
This does not work, and I cant figure out why:

Select max(My_date) from Mytable
where MY_ID = 'A'
and My_date < str_to_date('04/27/1988')
group by MY_ID

it always just gives me the max() record
0
Comment
Question by:GNOVAK
  • 2
3 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 38742339
I did not see any issue. What is the date are you getting with your query?
0
 

Author Comment

by:GNOVAK
ID: 38744260
I'm trying to get the second record as presented above:
A            4/1/1988 12:00:00 AM
and I keep getting :
A            4/28/1989 12:00:00 AM

almost like the Max() takes precedence
0
 

Accepted Solution

by:
GNOVAK earned 0 total points
ID: 38752790
Wierd.
If I do the following, I get the right date:

Select max(My_date) from Mytable
where MY_ID = 'A'
and My_date < '1988-27-04')

However if my data looks like this:
MY_ID    MY_DATE                             PRICE
A             3/6/1987 12:00:00 AM       1
A            4/1/1988 12:00:00 AM         2
A            4/28/1989 12:00:00 AM       3
B            3/6/1987 12:00:00 AM         5
B           4/1/1988 12:00:00 AM          6

and I query  using:
Select max(My_date), PRICE from Mytable
where MY_ID = 'A'
and My_date < '1988-27-04')

I get the right date (4/1/88) but get the first price (1)
How do I query to get the price corresponding to the date retrieved?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question