Solved

SQL return the closest matching date

Posted on 2013-02-03
8
772 Views
Last Modified: 2013-02-03
I have a date field that I want SQL to query in a optimized fashion and return the closest match, any suggestion?
0
Comment
Question by:Mazdajai
  • 5
  • 3
8 Comments
 
LVL 11

Expert Comment

by:tel2
ID: 38849726
Hi Mazdajai,

Are you saying you have a date field in a table and you want to find the record which contains the date which is closest to a date supplied by the user?

If so, how about you:
- subtract one date field from the other,
- sort the absolute value of those differences,
- and pick the lowest one (using "LIMIT 1").

Or do I misunderstand your requirements?
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 38849744
Yes, I solved it with LIMIT 1, but I am getting an error with 'mybirthdate does not exist' when I try to use it in the sub query:

with mybirthdate as (select birthdate from table1 t1
where birthdate >= '1960-01-01' and id <= 1000
order by birthdate
limit 1)
select *
from table2 t2
join t1 on t1.id = t2.id
where t2.birthdate = mybirthdate

ERROR: column "mybirthdate" does not exist
0
 
LVL 11

Expert Comment

by:tel2
ID: 38849774
Q1. So are you saying you've already solved the problem outlined in your original question?

Q2. How is the above SQL query related to your original question?
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 38849789
It is somehow related because I am trying to use it in a sub query.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:tel2
ID: 38849793
What is the answer to Q1, please Mazdajai.
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 38849797
Q1 - yes
0
 
LVL 11

Accepted Solution

by:
tel2 earned 500 total points
ID: 38849801
Good.  Please accept my answer and open another question to cover your new problem.

Thanks Mazdajai
0
 
LVL 11

Expert Comment

by:tel2
ID: 38849815
Thanks for the points.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

10 Experts available now in Live!

Get 1:1 Help Now