Solved

MySQL maximum date before a given date

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now