MaxDate and Group (1 Viewer)

kenrav

New member
Local time
Today, 08:19
Joined
Jun 20, 2020
Messages
29
I have the following table (attached):

1665938366018.png


I'd like to create a query that displays the Max(OrderDate) details by Customer:

OrderDate Customer ItemNum SerialNum
10/3/2022 Andy 300 323456780
10/3/2022 Betty 900 923456789

I've read a few articles but can't get it to work.

Thanks!
 

Attachments

  • Test.accdb
    1.1 MB · Views: 62

bob fitz

AWF VIP
Local time
Today, 16:19
Joined
May 23, 2011
Messages
4,727
Try:
SELECT TOP 1 Table1.OrderDate, Table1.Customer, Table1.ItemNum, Table1.SerialNum
FROM Table1
GROUP BY Table1.OrderDate, Table1.Customer, Table1.ItemNum, Table1.SerialNum
ORDER BY Table1.OrderDate DESC;
 

kenrav

New member
Local time
Today, 08:19
Joined
Jun 20, 2020
Messages
29
Bob,

Thanks for your help. Your solution works perfectly based on my previous data.

Unfortunately, I forgot to mention that not all customers will have the same order dates. My bad. As such, the following set of records:

1665944711073.png


only returns the following:

1665944834199.png


Again, my mistake. What I really need is for it to return the last order for all each customer.

I'm pretty sure the solution involves Max(OrderDate). I did this several years ago but can't find my old code.

Again, thanks for helping me.

I've attached an updated set of data for anyone willing to help.

Thanks!

Ken
 

Attachments

  • Test.accdb
    1.1 MB · Views: 70

Gasman

Enthusiastic Amateur
Local time
Today, 16:19
Joined
Sep 21, 2011
Messages
14,323
I can get your requirements, but had to count the other fields as I used a Totals query?
1665946362836.png

Code:
SELECT Max(Table1.OrderDate) AS MaxOfOrderDate, Table1.Customer, Count(Table1.ItemNum) AS CountOfItemNum, Count(Table1.SerialNum) AS CountOfSerialNum
FROM Table1
GROUP BY Table1.Customer;
 

MarkK

bit cruncher
Local time
Today, 08:19
Joined
Mar 17, 2004
Messages
8,183
I think you need a subquery (named qMax) to get your max dates first, like...
SQL:
SELECT Max(Table1.OrderDate) AS MaxOfOrderDate, Table1.Customer
FROM Table1
GROUP BY Table1.Customer;
Then use that to return the qualifying rows....
SQL:
SELECT Table1.OrderDate, Table1.Customer, Table1.ItemNum, Table1.SerialNum
FROM qMax INNER JOIN Table1 ON (qMax.Customer = Table1.Customer) AND (qMax.MaxOfOrderDate = Table1.OrderDate);
 

XPS35

Active member
Local time
Today, 17:19
Joined
Jul 19, 2022
Messages
159
Try
Code:
SELECT * FROM Table1 INNER JOIN 
(SELECT Table1.Customer, Max(Table1.OrderDate) AS MaxOrderDate FROM Table1 GROUP BY Table1.Customer)  AS Q1 
ON (Table1.Customer = Q1.Customer) AND (Table1.OrderDate = Q1.MaxOrderDate);
 

kenrav

New member
Local time
Today, 08:19
Joined
Jun 20, 2020
Messages
29
Try
Code:
SELECT * FROM Table1 INNER JOIN
(SELECT Table1.Customer, Max(Table1.OrderDate) AS MaxOrderDate FROM Table1 GROUP BY Table1.Customer)  AS Q1
ON (Table1.Customer = Q1.Customer) AND (Table1.OrderDate = Q1.MaxOrderDate);

Thanks everyone for your help!
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 16:19
Joined
Sep 21, 2011
Messages
14,323
Thanks everyone for your help! XPS35's solution was easiest to implement.
No different to MarkK's really, just in one step?
When you do not know what you are doing, breaking it down into small steps tends to get you there?
 

kenrav

New member
Local time
Today, 08:19
Joined
Jun 20, 2020
Messages
29
No different to MarkK's really, just in one step?
When you do not know what you are doing, breaking it down into small steps tends to get you there?
You're absolutely correct.
 

XPS35

Active member
Local time
Today, 17:19
Joined
Jul 19, 2022
Messages
159
No different to MarkK's really, just in one step?
When you do not know what you are doing, breaking it down into small steps tends to get you there?
You are right. I missed MarkK's post.

You are also (partly) right about breaking down in small steps. That is how I build this query. First make two query's and then integrate them. In order to maintain an overview, one query is ultimately better.
If you don't know what you're doing, it doesn't matter which solution you use :p
 

Users who are viewing this thread

Top Bottom