473,473 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Every customer's last invoice

Hi there! I have two tables:

Customer (ID, Name)
Invoice (ID, Date, Customer, Amount)

and want to select only the *last* invoice for each customer. It's easy
to get each customer's last invoice date:

SELECT Customer.Name, MAX(Invoice.Date)
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Customer
GROUP BY Customer.Name

but I run into trouble when I try to include the Invoice.Amount. How to
do this in one SQL statement? I came up with:

SELECT Customer.Name, Invoice.Date, Invoice.Amount
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Customer
WHERE Invoice.ID IN
(
SELECT MID(key, INSTR(key, ":") + 1) FROM
(
SELECT Customer, MAX(CLNG(Invoice.Date) & ":" & CLNG(Invoice.ID))
AS key
FROM Invoice
GROUP BY Customer
)
)

which leaves me somewhat unsatisfied. (And it won't work if the same
customer is invoiced twice on the same day.) There's got to be a more
elegant way to do this. Any ideas?
Jun 20 '06 #1
1 4801
See:
Getting a related field from a GroupBy (total) query
at:
http://www.mvps.org/access/queries/qry0020.htm
for 4 approaches to this query.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Herman Beeksma" <he****@opticrop.nl> wrote in message
news:9d***************************@news.concepts.n l...
Hi there! I have two tables:

Customer (ID, Name)
Invoice (ID, Date, Customer, Amount)

and want to select only the *last* invoice for each customer. It's easy to
get each customer's last invoice date:

SELECT Customer.Name, MAX(Invoice.Date)
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Customer
GROUP BY Customer.Name

but I run into trouble when I try to include the Invoice.Amount. How to do
this in one SQL statement? I came up with:

SELECT Customer.Name, Invoice.Date, Invoice.Amount
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Customer
WHERE Invoice.ID IN
(
SELECT MID(key, INSTR(key, ":") + 1) FROM
(
SELECT Customer, MAX(CLNG(Invoice.Date) & ":" & CLNG(Invoice.ID)) AS
key
FROM Invoice
GROUP BY Customer
)
)

which leaves me somewhat unsatisfied. (And it won't work if the same
customer is invoiced twice on the same day.) There's got to be a more
elegant way to do this. Any ideas?

Jun 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: The Blob | last post by:
Hi all, I need sone help on generating a SQL statement i had 3 tables now Customer >> Name >> Acct No >> Address
1
by: The Blob | last post by:
Hi all, I have a problem trying to generate the u/m list of customer. I am trying to generate a list of customer whoes last commence date is jan 04 to current. It is part of a billing...
2
by: solar | last post by:
I have a problem with building a totals query showing data for the last date only.Under the last date i understand the last date of field invoicedate.This invoice date is in the table orders....
2
by: lcrunicorn | last post by:
I am trying to figure out the best way to get an alert in my Access 2002 Database when an invoice that is for an active vendor has not been received. Ex. I receive invoices on a daily bases. If...
4
by: freefly_xml | last post by:
I want to test to see if I am on the last page of a document. In this example it is an invoice. I want to print a different table in REGION AFTER when I am on the last page. I have tried many...
3
by: syargus | last post by:
I have a database that was built for us by a gentlemen from Sri Lanka. The database was supposed to have an invoice that could be edited after it was created and also there was supposed to be a...
3
by: dancole42 | last post by:
I'm self-taught in Access, and as such I'm missing large chunks of knowledge, so I'm hoping someone here with some training can help me. Right now I have an Invoice form with a Customer subform....
3
by: Ly Cao | last post by:
Please shine the light, how do you place the total on the last page of the invoice? how do you find out the it is a last page of the detail lines then place the total of the invoice into that last...
0
by: Nagel Oxles | last post by:
Customer details are kept in the Customers_table with CompanyName as the PK. Tasks_table has TaskName, RatePerHour, and TaskCode (Two letter code indexed no duplicates) as PK. Jobs_table has...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.