Solved

SQL return the closest matching date

Posted on 2013-02-03
8
773 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 12

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 12

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
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.

 
LVL 12

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 12

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 12

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

912 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

18 Experts available now in Live!

Get 1:1 Help Now