Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Subqueries 1

Status
Not open for further replies.

jchewsmith

Technical User
Nov 20, 2006
161
US
I need to create a subquery to get the MIN(INVDATE) for an order and return the MINDate in the query.

Order Table = order id, cust id
Inv table = cust id, order id and Invdate

I need my query to display all invoices for a customer and the oldest invoice for that customer
 
Code:
SELECT Orders.*,
       Inv.MinDate
FROM Orders
INNER JOIN (SELECT OrderId, MIN(Invdate) AS MinDate
                  FROM Inv
             GROUP BY OrderId) Inv ON Orders.OrderId = Inv.OrderId

Borislav Borissov
VFP9 SP2, SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top