I took your sample data and modified it to show more items and another Customer.
My understanding is you want the latest Date per Item per Customer
Here is the modified test data:
ID |
cusid |
ItemID |
InvoiceDate |
SalesOrderID |
USPInvoiced |
1 |
583 |
10C1 |
15-Aug-2017 |
3766266 |
1.42 |
2 |
583 |
10C2 |
28-Mar-2017 |
3352616 |
1.33 |
3 |
583 |
10C1 |
22-Jul-2016 |
2768198 |
1.32 |
4 |
526 |
11L9 |
08-Feb-2014 |
2769308 |
3.45 |
5 |
526 |
11L9 |
08-Dec-2015 |
2669308 |
3.21 |
Here is the query:
Code:
SELECT Gpierce.cusid
, Gpierce.ItemID
, Max(Gpierce.InvoiceDate) AS MaxOfInvoiceDate
FROM Gpierce
GROUP BY Gpierce.cusid, Gpierce.ItemID
ORDER BY ItemID;
Here is the result:
cusid |
ItemID |
MaxOfInvoiceDate |
583 |
10C1 |
15-Aug-2017 |
583 |
10C2 |
28-Mar-2017 |
526 |
11L9 |
08-Dec-2015 |