Avatar of steverguy
steverguy
 asked on

SQL Sum Question

I'm connecting to a QuickBooks database using ODBC and MS Access.  I'm trying to use SQL to pull data in a way quickbooks will not allow.  For example, I would like to get a list of people who in the past year have ordered 3 or more times with a value of over $x.

There are several feilds in the Sales Table, which seams to be the only table I'll need to get this information.  The names of the fields are:
1. ShipDate (determining when ordered)
2. Subtotal (determining what amount)
3. CustomerRefListID (the Customer's ID)

I'm kind of lost as to where to even begin.  

Select *, Sum(CustomerRefListID) as CustTotal from Sales WHERE  CustTotal > 3...

Of course I know this won't work, since CustTotal can't be determined like that...  Even if it could be I'm not sure I'm approaching it correctly.  Any advice, guidence or suggestions would be appreciated!!!

Thanks!
Microsoft Access

Avatar of undefined
Last Comment
steverguy

8/22/2022 - Mon
Patrick Matthews

Hi steverguy,

There is probably a more elegant way to do this, but this should work:

SELECT s2.CustomerRefListID, Count(*)
FROM
    (SELECT s.CustomerRefListID, s.ShipDate, s.Subtotal
    FROM Sales AS s
    WHERE (s.ShipDate BETWEEN #9/1/2005# AND #8/31/2006#) AND s.Subtotal >= 50) AS s2
GROUP BY s2.CustomerRefListID
HAVING Count(*) >= 3

Regards,

Patrick
ASKER CERTIFIED SOLUTION
mcallarse

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.
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
steverguy

ASKER
Thanks for your help, mcallarse!  Trying to find SQL Tutorials that cover things like this is tough.  I tried to give you partial credit Patrick, but I don't think I did it right.  Sorry...

Thanks again for the help!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy