473,748 Members | 10,649 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.Dat e)
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Custome r
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.Custome r
WHERE Invoice.ID IN
(
SELECT MID(key, INSTR(key, ":") + 1) FROM
(
SELECT Customer, MAX(CLNG(Invoic e.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 4831
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****@opticro p.nl> wrote in message
news:9d******** *************** ****@news.conce pts.nl...
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.Dat e)
FROM Customer INNER JOIN Invoice ON Custimer.ID = Invoice.Custome r
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.Custome r
WHERE Invoice.ID IN
(
SELECT MID(key, INSTR(key, ":") + 1) FROM
(
SELECT Customer, MAX(CLNG(Invoic e.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
2444
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
2927
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
1869
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
2497
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. Does anyone have any idea on how I can go about doing this? If I am not clear, please ask me...
4
7794
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 the xml, xsl and pdf look like are here: http://www.bangboompow.com/xml/invoice/
3
1571
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 and apply it towards the balances. It was also supposed to show backorders. Also on the invoice...
3
2379
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 up in the Customer subform. How do I create a system where a user could start a new invoice, then
3
2837
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
1456
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 as autonumber for PK. JobName (PK for
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.