Solved

Matching approximate dates between two queries

Posted on 2011-03-11
3
395 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the quick responses!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

8 Experts available now in Live!

Get 1:1 Help Now