?
Solved

MySQL maximum date before a given date

Posted on 2013-01-03
5
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
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

777 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