A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

GROUP BY question



 
 
Thread Tools Display Modes
  #1  
Old September 11th, 2008, 03:11 PM posted to microsoft.public.access.queries
0to60[_2_]
external usenet poster
 
Posts: 5
Default GROUP BY question

Let's say I have an Invoices table setup and I want to find the most recent
invoice date per customer. I'd say:

SELECT Customer, Max(InvoiceDate)
FROM Invoices
GROUP BY Customer

Now what if I want to see the invoice number (or some other field of the
Invoices table) of those most recent invoices? In other words, the above
query tells me the date of the most recent invoice per customer. What if I
wanna know more information about that particular invoice? Do I have to do
a separate query?

  #2  
Old September 11th, 2008, 03:30 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 2,364
Default GROUP BY question

You can use that query as a subquery. The SQL statement would look like
the following.

SELECT Invoices.*
FROM Invoices INNER JOIN
(SELECT Customer, Max(InvoiceDate) as Latest
FROM Invoices
GROUP BY Customer ) as x
ON Invoices.Customer = x.Customer
AND Invoices.InvoiceDate = x.Latest


'================================================= ===
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===


0to60 wrote:
Let's say I have an Invoices table setup and I want to find the most recent
invoice date per customer. I'd say:

SELECT Customer, Max(InvoiceDate)
FROM Invoices
GROUP BY Customer

Now what if I want to see the invoice number (or some other field of the
Invoices table) of those most recent invoices? In other words, the above
query tells me the date of the most recent invoice per customer. What if I
wanna know more information about that particular invoice? Do I have to do
a separate query?

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 11:00 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.