Avatar of frimy
frimyFlag for United States of America asked on

Select only the maximum amount for each customer from a list MS 2010

I have list from all customers the daily open balance by date (I'm getting it from the Running Sum total).
I need to get only the largest Open Balance for any date for each customer.

I'm using created the following query.
It only works with little records.
But with a lot records I get an error "At most one record can be returned by this subquery"
Thanks in advance

MaxAmt: (SELECT top 1 [RunningSum ] FROM [qCustBal AddRunAmt] WHERE CustomerID=[Customer].[CustomerID] ORDER BY RunningSum Desc)
Microsoft Access

Avatar of undefined
Last Comment
frimy

8/22/2022 - Mon
Hamed Nasr

Try this for tbl (CustID, fld) to get maximum value of fld for each CustID.

SELECT a.CustID, (SELECT MAX(b.fld)  FROM tbl AS b WHERE b.CustID = a.CustID)
FROM tbl AS a
GROUP BY a.CustID

Open in new window

Sharath S

Provide some sample data and expected result.
ASKER
frimy

Hamed,

it works very good.
but it's running very slow.

I have 5,630 records (test only)
to add a running total it takes about 3 seconds (from Dale)
But to the get the maximum value for each customer (the greatest amount), it takes about 9 minutes to run.
Is it possible to make it faster?
Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
aikimark

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
aikimark

If you have other needs, the ordinary Group By query can be part of the FROM clause of another (higher level) query, joined on the custid field.
Hamed Nasr

I go with aikimark's comment, for this question.
ASKER
frimy

aikimark, it works beautiful, it's funny I didn't think of it because I use it all the time.
maybe because I was focusing on the Add Running total query.
thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.