473,538 Members | 6,363 Online
Bytes | Software Development & Data Engineering Community
+ 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 4810
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
2428
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
2906
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 system which the customer come in and pay for their season parking in carpark. They can pay for various period shortest being 1 week.
2
1858
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. Therefore in the column of the query i have entered : DMax("InvoiceDate";"Orders") I have also tried with : DayNumber: DatePart('d';)
2
2490
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 an invoice was received last month, it is assumed that if the account number from that vendor is active, I am waiting to receive the invoice. ...
4
7773
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 variations, no luck yet. It seems like it should be an easy thing to do with xsl:choose. Any ideas? Here is one of my attempts. More detail of what...
3
1564
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 feature that when the invoice was created that it would also create a balance for the customer and allow me to record the payments the customer makes...
3
2370
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. Right now it's set up so that someone can enter a customer ID, say, 12345, into the invoice form and customer 12345's name, address, etc. shows...
3
2831
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 page.
0
1446
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 JobNum as autonumber for PK. JobName. CompanyName (PK for Customer_table) and several other details applicable to jobs. Time_table has SubFormID...
0
7536
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5829
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5226
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3358
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
933
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
591
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.