Link to home
Start Free TrialLog in
Avatar of grwallace
grwallace

asked on

Join on only the last record in a table

I need to create a query from a sales order table linked to an invoice table which returns only the last invouce for the order. It is possible for an order to be invoiced then credited again then re-invoiced giving 3 entries in the Invoice table for one Order. I can easily excluse the Credit leaving 2 entries in the Invoice table but I only wish to return the most recent invoice, which wil always have the highest Id

If I say

SELECT Orders.[Order Number], Invoices.InvoiceId, Invoices.[Invoice Number] FROM Orders INNER JOIN Invoices on Invoices.OrdersId = Orders.OrdersId

it will return the three records.if there are three records present. I would like to only return the one with the highest Invoices.InvoiceId

Any ideas anyone?
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
ASKER CERTIFIED SOLUTION
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 grwallace
grwallace

ASKER

I gave LJZ the most points as this was a direct answer to a direct question, however Angelll also deserves some credit for an excelent article which I will use for refrence in the future