SQL 2000> Group By Customer for Max(BillDate)

Hello,
I have a single table with each record being bill information.
Example (I have excluded some columns that would not be relevant)
BILLID (int) (Primary Key)
CUST_ACCT_NO (int)
BILL_AMOUNT (money)
BILL_DATE (Datetime)

Each customer will have several bills (one each month).  I am trying to find the amount of the last bill only. I would like to list the CUST_ACCT_NO, BILLAMOUNT, BILL_DATE only where the bill date for each customer is the max bill date (their last bill only).

I know this is somewhat simple, but I am just not getting it.
Thanks in advance!
LVL 7
Joel MillerDevOps EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ErnariashCommented:
I hope this will hepl you


SELECT * FROM BILLS B 
WHERE EXISTS 
( SELECT 1 FROM ( 
SELECT CUST_ACCT_NO, MAX(BILL_DATE) LASTBILLDATE FROM BILLS 
) L 
WHERE B.CUST_ACCT_NO = L.CUST_ACCT_NO AND B.LASTBILLDATE = L.LASTBILLDATE 
)

Open in new window

0
ErnariashCommented:
Sorry I missed the group by ...
SELECT * FROM BILLS B 
WHERE EXISTS 
( SELECT 1 FROM ( 
		SELECT CUST_ACCT_NO, MAX(BILL_DATE) LASTBILLDATE FROM BILLS 
		GROUP BY CUST_ACCT_NO
) L 
WHERE B.CUST_ACCT_NO = L.CUST_ACCT_NO AND B.LASTBILLDATE = L.LASTBILLDATE 
)

Open in new window

0
ErnariashCommented:

This query will give you the last LAST BILLDATE by customer,
but since your BILLID is a PK your las Billid sould be your LAST BILLDATE (faster query)

--------Last bill id 
SELECT * FROM BILLS B 
WHERE EXISTS 
( SELECT 1 FROM ( 
		SELECT CUST_ACCT_NO, MAX(BILLID) LASTBILLID 
		FROM BILLS 
		GROUP BY CUST_ACCT_NO 
) L 
WHERE B.CUST_ACCT_NO = L.CUST_ACCT_NO AND B.BILLID = L.LASTBILLID 
) 
 
--------Last bill date 
SELECT * FROM BILLS B 
WHERE EXISTS 
(	SELECT 1 FROM 
	( 
			SELECT CUST_ACCT_NO, MAX(BILL_DATE) LASTBILLDATE FROM BILLS 
			GROUP BY CUST_ACCT_NO 
		) L 
	WHERE B.CUST_ACCT_NO = L.CUST_ACCT_NO AND B.BILLDATE = L.LASTBILLDATE 
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joel MillerDevOps EngineerAuthor Commented:
Thank you so much for the quick response. I tried your first solution and wondered what I did wrong. Then I saw your immediate reply. :)

Everything worked great and I really appreciate it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.