Solved

SQL return the closest matching date

Posted on 2013-02-03
8
776 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
[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
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 21

Author Comment

by:Mazdajai
ID: 38849789
It is somehow related because I am trying to use it in a sub query.
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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