Link to home
Start Free TrialLog in
Avatar of IT Guy
IT Guy

asked on

SQL - Subtract OLDEST record in TABLE 2 from NEWEST record from TABLE 1 (Without Cursor)

The best way I can explain my question is with the graphic below.

I want to select the OLDEST record from TABLE 2 and the LATEST record from TABLE 1 and find the difference in a query. Obviously I can loop through my items and select TOP 1 from both tables and do the calculation, but as usual, this takes too long.

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT Guy
IT Guy

ASKER

Eric.

You have taught me something today. I didn't fully understand the WITH statement or the ROW_NUMBER function until today, and you taught me.

After a little fiddling around it worked, and I'm very grateful. Thank you.