show last record from related table in access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have a one to many relationship for a customers and orders table. how do i
get the last entered order for each customer
 
bri said:
i have a one to many relationship for a customers and orders table. how do i
get the last entered order for each customer

Unless you have some field in each record in [orders] indicating the
date (and maybe time) of each order, you won't be able to determine
which was last entered.

Assuming you do have that, you can use a Query like the following one
(which targets Tables in the "Northwind Traders" database):

SQL view:

SELECT DISTINCTROW Orders.CustomerID, Orders.OrderDate,
Orders.OrderID, Orders.EmployeeID, Orders.RequiredDate,
Orders.ShippedDate, Orders.ShipVia, Orders.Freight,
Orders.ShipName, Orders.ShipAddress, Orders.ShipCity,
Orders.ShipRegion, Orders.ShipPostalCode,
Orders.ShipCountry, Customers.CompanyName,
Customers.Address, Customers.City, Customers.Region,
Customers.PostalCode, Customers.Country
FROM Customers INNER JOIN (Orders INNER JOIN
Orders AS Orders_1
ON Orders.CustomerID = Orders_1.CustomerID)
ON Customers.CustomerID = Orders.CustomerID
GROUP BY Orders.CustomerID, Orders.OrderDate,
Orders.OrderID, Orders.EmployeeID,
Orders.RequiredDate, Orders.ShippedDate,
Orders.ShipVia, Orders.Freight, Orders.ShipName,
Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion,
Orders.ShipPostalCode, Orders.ShipCountry,
Customers.CompanyName, Customers.Address,
Customers.City, Customers.Region,
Customers.PostalCode, Customers.Country
HAVING (((Orders.OrderDate)
=Max([Orders_1].[OrderDate])));

which, in Query Datasheet View, looks like this (at the beginning):

Customer Order Date Order ID ...
---------------------------- ----------- --------
Alfreds Futterkiste 09-Apr-1998 11011 ...
Ana Trujillo Emparedados ... 04-Mar-1998 10926 ...
Antonio Moreno Taquería 28-Jan-1998 10856 ...
...

-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.
 
Could be an order number if a date is note available. In fact it would be
better because it may be possilbe there would be two orders for the same
customer on the same date.

Vincent Johns said:
bri said:
i have a one to many relationship for a customers and orders table. how do i
get the last entered order for each customer

Unless you have some field in each record in [orders] indicating the
date (and maybe time) of each order, you won't be able to determine
which was last entered.

Assuming you do have that, you can use a Query like the following one
(which targets Tables in the "Northwind Traders" database):

SQL view:

SELECT DISTINCTROW Orders.CustomerID, Orders.OrderDate,
Orders.OrderID, Orders.EmployeeID, Orders.RequiredDate,
Orders.ShippedDate, Orders.ShipVia, Orders.Freight,
Orders.ShipName, Orders.ShipAddress, Orders.ShipCity,
Orders.ShipRegion, Orders.ShipPostalCode,
Orders.ShipCountry, Customers.CompanyName,
Customers.Address, Customers.City, Customers.Region,
Customers.PostalCode, Customers.Country
FROM Customers INNER JOIN (Orders INNER JOIN
Orders AS Orders_1
ON Orders.CustomerID = Orders_1.CustomerID)
ON Customers.CustomerID = Orders.CustomerID
GROUP BY Orders.CustomerID, Orders.OrderDate,
Orders.OrderID, Orders.EmployeeID,
Orders.RequiredDate, Orders.ShippedDate,
Orders.ShipVia, Orders.Freight, Orders.ShipName,
Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion,
Orders.ShipPostalCode, Orders.ShipCountry,
Customers.CompanyName, Customers.Address,
Customers.City, Customers.Region,
Customers.PostalCode, Customers.Country
HAVING (((Orders.OrderDate)
=Max([Orders_1].[OrderDate])));

which, in Query Datasheet View, looks like this (at the beginning):

Customer Order Date Order ID ...
---------------------------- ----------- --------
Alfreds Futterkiste 09-Apr-1998 11011 ...
Ana Trujillo Emparedados ... 04-Mar-1998 10926 ...
Antonio Moreno Taquería 28-Jan-1998 10856 ...
...

-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top