Solved

Matching approximate dates between two queries

Posted on 2011-03-11
3
397 Views
Last Modified: 2013-12-25
Hello-

Is there sql that will allow for an approximate match on dates between two tables with a one-to-many relationship?

Essentially, this mimics the Microsoft Excel vlookup funtion using the 'True' designation of finding an approximate match.  I've attached a simple example.

Thanks!
Todd.  
EExchange.pdf
0
Comment
Question by:TCristiano
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 63 total points
ID: 35113517
Not sure how approximate you need it. This requires the two dates to be within 100 days of each other. It produces the result that you show in the PDF.


SELECT Table1.[Asset Name], Table1.[Acquisition Date], Table1.Cost, Table2.Year, Table2.Index
FROM Table1, Table2
WHERE (Abs([Acquisition Date]-[Table2].[Year])<100);
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 62 total points
ID: 35113622
Can you check this?
SELECT * 
  FROM Table1 AS t1, 
       Table2 AS t2 
 WHERE ABS(DATEDIFF(d,t1.Acquisition_Date,t2.Year)) = (SELECT MIN(ABS(DATEDIFF(d,t1.Acquisition_Date,t2.Year)))
                                                         FROM Table1 AS t3, 
                                                              Table2 AS t4 
                                                        WHERE t1.AssetName = t3.t1.AssetName 
                                                              AND t1.Acquisition_Date = t3.Acquisition_Date
                                                              AND t1.Cost = t3.Cost)

Open in new window

0
 

Author Closing Comment

by:TCristiano
ID: 35131188
Thanks for the quick responses!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

932 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

19 Experts available now in Live!

Get 1:1 Help Now