Solved

MySQL maximum date before a given date

Posted on 2013-01-03
5
195 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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