Solved

MySQL maximum date before a given date

Posted on 2013-01-03
5
190 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
5 Comments
 
LVL 40

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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